I have a static function in a class that needs to be called from several child classes. I need a constant from the calling child class that will be available in this function. To make these constants available elsewhere, the child classes have a function that returns the value of this constant (php 5.2.9).
However, when in the parent class I cannot access this constant, not the function or directly. Is this possible in php 5.2.9 or do I need to pass it as an argument?
This is a simple version of the code:
abstract class ParentClass { static function DoSomething() { $not_working = self::show_const(); $not_working_either = self::SOME_CONST; return 'working'; } } class ChildClass extends ParentClass { const SOME_CONST = 'some string'; function show_const() { return self::SOME_CONST; } } $result = ChildClass::DoSomething();
Edit: created error:
- Call the undefined method ParentClass :: show_const () (for a function)
- Undefined class constant 'SOME_CONST' (using self :: SOME_CONST)
object php class parent-child self
jeroen
source share