When creating classes or calling static methods from within namespaces using variables, you need to keep in mind that they require the full namespace in order for the appropriate class to be used; you CANNOT use an alias or short name, even if it is called within the same namespace. Neglecting to take this into account can cause your code to use the wrong class, throw a fatal missing class exception, or throw errors or warnings.
In these cases, you can use the magic constant __NAMESPACE__, or specify the full namespace and class name directly. The function class_exists also requires the full namespace and class name, and can be used to ensure that a fatal error won't be thrown due to missing classes.
<?php
namespace Foo;
class Bar {
public static function test() {
return get_called_class();
}
}
namespace Foo\Foo;
class Bar extends \Foo\Bar {
}
var_dump( Bar::test() ); $bar = 'Foo\Bar';
var_dump( $bar::test() ); $bar = __NAMESPACE__ . '\Bar';
var_dump( $bar::test() ); $bar = 'Bar';
var_dump( $bar::test() );