17

How can I retrieve a class namespace automatically?

The magic var __NAMESPACE__ is unreliable since in subclasses it's not correctly defined.

Example:

class Foo\bar\A -> __NAMESPACE__ === Foo\bar

class Ping\pong\B extends Foo\bar\A -> __NAMESPACE__ === Foo\bar (it should be Ping\pong)

ps: I noticed the same wrong behavior using __CLASS__, but I solved using get_called_class()... is there something like get_called_class_namespace()? How can I implement such function?

UPDATE:
I think the solution is in my own question, since I realized get_called_class() returns the fully qualified class name and thus I can extract the namespace from it :D ...Anyway if there is a more effective approach let me know ;)

4

3 Answers 3

21

The namespace of class Foo\Bar\A is Foo\Bar, so the __NAMESPACE__ is working very well. What you are looking for is probably namespaced classname that you could easily get by joining echo __NAMESPACE__ . '\\' . __CLASS__;.

Consider next example:

namespace Foo\Bar\FooBar;

use Ping\Pong\HongKong;

class A extends HongKong\B {

    function __construct() {
        echo __NAMESPACE__;
    }
}

new A;

Will print out Foo\Bar\FooBar which is very correct...

And even if you then 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 the nested class within the main that is nesting it, simply 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), '\\'));
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

They're using __NAMESPACE__ in the parent class, but they want the namespace of the child class.
@MaximilianRuta Well, thank You for Your edit, but please, in the future only correct what is necessary to be corrected. All occurrences of You changed to you? This is my style of writing and to show some politeness so if You do not mind, let me keep my style of expressing myself. Thanks! Also thank to reviewers for approving such minor and not so much correct edit request... Well done!
The problem is that i have to change at least n chars for an edit request. Thats the reason why i had to change something else in your post.
Why do You write like this?
Why do I write like this?
14

In PHP 5.5, ::class is available which makes things 10X easier. E.g. A::class

2 Comments

or get_class() for dynamic classes.
11

Use Reflection class.

$class_name = get_class($this);

$reflection_class = new \ReflectionClass($class_name);

$namespace = $reflection_class->getNamespaceName();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.