Late Static Binding namespace resolution in PHP

If you try to get the namespace of the child class that you are in using the __NAMESPACE__ keyword, you will have the same problem that you do using functions like get_class() in that you will get the namespace (or class) where the current method you are calling is defined.

$namespace = __NAMESPACE__;
$class = get_class();

You can use get_called_class() to resolve this for Late Static Binding with classes, but no such function exists for namespaces that I can find.

$class = get_called_class();

However you can work this out by using the fully-qualified class name, and deducing the namespace from that; if you get the Late Static Binding class name, then you can get the Late Static Binding namespace.

Add comment