This is how I wanted to do it which would work in PHP 5.3.0+
<?php
class MyClass
{
const CONSTANT = 'Const var';
}
$classname = 'MyClass';
echo $classname::CONSTANT; // As of PHP 5.3.0
?>
But I'm restricted to using PHP 5.2.6. Can anyone think of a simple way to simulate this behavior without instantiating the class?
You can accomplish this without using eval
in pre-5.3 code. Just use the constant
function:
<?php
class MyClass
{
const CONSTANT = 'Const var';
}
$classname = 'MyClass';
echo constant("$classname::CONSTANT");
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With