Example:
class LOL{
const
FOO = 1;
}
$x = new LOL;
$arr = array('x' => $x);
echo $x::FOO; // works
echo $arr['x']::FOO; // works too
But if I make my class instance a property, I can't access the constant anymore:
class WWW{
protected $lol;
public function __construct($lol){
$this->lol= $lol;
}
public function doSMth(){
echo $this->lol::FOO; // fail. parse error.. wtf
}
}
$w = new WWW;
$w->doSMth();
:(
I know I can just do echo LOL::FOO
, but what if the class name is unknown? From that position I only have access to that object/property, and I really don't want that WWW class to be "aware" of other classes and their names. It should just work with the given object
You can do this much easier by assigning the lol
property to a local variable, like so:
public function doSMth(){
$lol = $this->lol;
echo $lol::FOO;
}
This is still silly, but prevents having to use reflections.
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