Is it possible to get the value of an overridden NON static member variable of a parent class?
I understand that to get the value of a STATIC member variable you use self::$var1 or ClassName::$var1, but how do you get the value of a NON static member variable?
For instance…
class One
{
public $var1 = 'old var';
}
class Two extends One
{
public $var1 = 'new var';
public function getOldVar()
{
//somehow get old var
}
}
Thanks so much in advance!
Nope. Once you’ve overridden a non-static property value it’s gone. You can’t use the
parent::syntax with non-static properties like you can with methods.However, using the
statickeyword you can utilize PHP’s late static binding capabilities to access a static parent property because the static values are bound to the class in which they’re assigned:Note that you cannot override a non-static property with a static one in a child class to achieve what you’re attempting because PHP (and all other scripting languages, I believe) uses the same table to store property names. This is just a limitation of the language.