I have a situation similar to the following code:
class ParentClass
{
public static $property = 'parentValue';
public static function doSomethingWithProperty() {
echo 'Method From Parent Class:' . self::$property . "\n";
}
}
class ChildClass extends ParentClass
{
public static $property = 'childValue';
}
echo "Directly: " . ChildClass::$property . "\n";
ChildClass::doSomethingWithProperty();
Running this from the cli I get the output:
Directly: childValue
Method From Parent Class: parentValue
Is there a way to retrieve the static property defined in a subclass from a static method defined in the parent class?
Using self
keyword always reference same class.
To allow overriding of static property/method you have to use static
keyword. Your method should look like this
public static function doSomethingWithProperty()
{
echo 'Method From Parent Class:' . static::$property . "\n";
}