Reputation: 1391
How can I access static php variable with custom class name. In class c1 method hi() I need to access static variable of its child class. PHP < 5.3
class c1{
function hi(){
$cn=get_class($this);
echo $cn::$b; //need echo 5 here, but error
}
}
class c2 extends c1{
static public $b=5;
}
$c2=new c2();
$c2->hi();
Upvotes: 2
Views: 230
Reputation: 19164
You can use ReflectionClass
:
$cn=get_class($this);
$cl=new ReflectionClass($cn);
echo $cl->getStaticPropertyValue('b');
Or get_class_vars()
:
$cn=get_class($this);
$props=get_class_vars($cn);
echo $props['b'];
Upvotes: 4
Reputation: 44104
One way that popped into my mind is eval( "return $cn::\$b;" )
but use with care. Eval can create some nasty security holes if the input isn't sanitized correctly.
Upvotes: 1