CodeIgniter Forums
Class Name is variable does NOT work - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Class Name is variable does NOT work (/showthread.php?tid=77705)



Class Name is variable does NOT work - iot - 10-07-2020

This code work on CI3 but not on CI4
PHP Code:
class Foo
{
    public static $var2 1;
}

$classname 'Foo';
echo 
$classname::$var2// Outputs 1 
In CI4, it will return an error: Class 'Foo' not found
Could anyone please  tell me how to fix it. 

Thank you in advance.

Please note that if I change echo $classname::$var2; to echo Foo::$var2;, it will work/


RE: Class Name is variable does NOT work - ojmichael - 10-08-2020

This isn't any code specific to CI.. that's just plain vanilla PHP, so not sure what would be different between your configurations. I believe what you have should work, but alternatively you could try something like this.

PHP Code:
$reflector = new ReflectionClass($classname);
echo 
$reflector->getStaticPropertyValue($var2); 



RE: Class Name is variable does NOT work - MGatner - 10-08-2020

Your example definitely should work. That is, what you are trying to do is valid PHP as of 5.3. Pay attention to your namespace if you are doing it in CI4 because the local reference may not resolve in a variable name.


RE: Class Name is variable does NOT work - iot - 10-11-2020

Thank you!