Questions says it all really.
I have constants defined in my parent class. I have tried $this->CONSTANT_1
but it's not working.
class MyParentClass{ const CONSTANT_1=1 } class MyChildClass extends MyParentClass{ //want to access CONSTANT_1 }
I think you would need to access it like this:
self::CONSTANT_1;
or alternatively "parent", which will always be the value established in the parent class (i.e., the constant's immutability is maintained):
parent::CONSTANT_1;
One thing that is interesting to note is that you can actually override the const value in your child class.
class MyParentClass{ const CONSTANT_1=1; } class MyChildClass extends MyParentClass{ const CONSTANT_1=2; } echo MyParentClass::CONSTANT_1; // outputs 1 echo MyChildClass::CONSTANT_1; // outputs 2
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