I've found that by using ::class, I can get the fully qualified class name of a namespaced class:
namespace NameSpace {
class Foo { }
echo Foo::class;
}
// echoes 'NameSpace\Foo'
But I can't figure out a way to do this with a variable class name. Trying to treat the class keyword like a static property doesn't seem to work:
namespace NameSpace {
class Foo { }
$className = 'Foo';
echo $className::class;
}
// Parse error: syntax error, unexpected 'class'
Does anyone know if its possible to get the fully qualified class name dynamically like I'm trying to?
I want to be able to do this with classes from outside the current namespace:
namespace ReallyLongHardToWriteNameSpace {
class Foo { }
}
namespace NameSpace {
use ReallyLongHardToWriteNameSpace\Foo;
class Bar { }
echo Foo::class; // echoes 'ReallyLongHardToWriteNameSpace\Foo'
echo Bar::class; // echoes 'NameSpace\Bar'
foreach (['Foo', 'Bar'] as $className) {
echo $className::class; // Parse error: syntax error, unexpected 'class'
}
}