Welcome Guest, Not a member yet? Register   Sign In
Class Name is variable does NOT work
#1

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/
Reply
#2

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); 
Reply
#3

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.
Reply
#4

Thank you!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB