Today I ran into some strange problem, and even as a software engineer I donβt understand:
It seems you can access the class constant from an instance of an object, for example:
class a { const abc = 1; } $a = new a(); var_dump($a->abc);
This will output null instead of the expected 1. I was able to do the following:
class a { const abc = 1; } $a = new a(); var_dump(a::abc);
But in the context of a subordinate object that does not really know who the parent is, it is very unpleasant for me to do this:
class a { const abc = 1; } $a = new a(); $c = get_class($a); var_dump($c::abc);
Is it me, or is it completely stupid, if not, please enlighten me why this works.
EDIT
Another way to do this, but it is not better:
class a { const abc = 1; } class b { public function getA(){ return new a(); } } $b = new b(); $c = $b->getA(); var_dump($c::abc);
This last example is more like what I do and worry about ...
source share