The class Foo\Bar\A namespace class Foo\Bar\A is equal to Foo\Bar , so __NAMESPACE__ works very well. What you are looking for is probably the class name, which you can easily get by joining echo __NAMESPACE__ . '\\' . __CLASS__; echo __NAMESPACE__ . '\\' . __CLASS__; .
Consider the following example:
namespace Foo\Bar\FooBar; use Ping\Pong\HongKong; class A extends HongKong\B { function __construct() { echo __NAMESPACE__; } } new A;
Print Foo\Bar\FooBar , which is very correct ...
And even if you do
namespace Ping\Pong\HongKong; use Foo\Bar\FooBar; class B extends FooBar\A { function __construct() { new A; } }
it will echo Foo\Bar\FooBar , which again is very correct ...
EDIT: If you need to get the namespace of a nested class inside the main nested class, just use:
namespace Ping\Pong\HongKong; use Foo\Bar\FooBar; class B extends FooBar\A { function __construct() { $a = new A; echo $a_ns = substr(get_class($a), 0, strrpos(get_class($a), '\\')); } }
shadyyx
source share