Reputation: 5431
I currently encountered this namespace code block during code review that I looks something like this. I'm new to namespaces and examples I've seen in youtube and tutorials have no similar example.
I only understand the first namespace means the code block will be on global space. But what does the second namespace exactly mean?
namespace
{
class Logging{}
}
namespace Admin\Logging
{
class Logged_in
{
public function __construct()
{
/* some code here */
}
}
/* other classes here */
}
Thank you for your help.
Upvotes: 2
Views: 298
Reputation: 5431
To answer my own question, hope I got this right...
In layman's term, Admin\Logging means "where can I find this file"
|--admin
| |-- Logging.php
and that
namespace
{
class Logging {}
}
because both namespace are in the same file, it can serve as a library and be autoloaded...
Wew... I'm not really technical but that's how I understand it..... thanks all for your help.
Thanks to: http://www.mwop.net/blog/254-Why-PHP-Namespaces-Matter.html
Upvotes: 0
Reputation: 14730
The syntax for namespaces is similar to directory hierarchy: backslashes (\
) act as separators for subnamespaces.
Therefore the second notation indicates the definition of the Admin
namespace, and of the Logging
namespace within it.
See the documentation for more information.
Upvotes: 0
Reputation: 173552
The second declaration is a sub namespace or nested namespace.
In your example, the class Logged_in
would have the canonical name of \Admin\Logging\Logged_in
Upvotes: 1