Instantiating class by string using PHP 5.3 namespaces
Posted
by
Kevin
on Stack Overflow
See other posts from Stack Overflow
or by Kevin
Published on 2011-02-21T23:10:41Z
Indexed on
2011/02/21
23:24 UTC
Read the original article
Hit count: 182
I can't get around an issue instantiating a new class by using a string variable and PHP 5.3. namespaces. For example, this works;
$class = 'Reflection';
$object = new $class();
However, this does not;
$class = '\Application\Log\MyClass';
$object = new $class();
A fatal error gets thrown stating the class cannot be found. However it obviously can be instantiated if using the FQN i.e.;
$object = new \Application\Log\MyClass;
I've found this to be aparrent on PHP 5.3.2-1 but not not in later versions. Is there a work around for this?
© Stack Overflow or respective owner