I have a problem with accessing parents (non-static) property in child class static method. I’ve tried these as below:
class Parent
{
protected $nonStatic;
// Other methods and properties
}
class Child extends Parent
{
public static function staticFunc()
{
$test = $this->nonStatic; # Using $this when not in object context
$test = self::$nonStatic; # Access to undeclared static property
$test = parent::$nonStatic # Access to undeclared static property
}
}
I checked similar question in stackoverflow, but i didn’t get any working solution
P.S. Sorry about typos, and code above is a dummy example
Obviously a static method won’t know what a non-static parent property is. It doesn’t know what instance of the object the call is being made on – so it can’t know that objects parent. Either set the parent prop to static or pass in the instance of the child object to the method and call passedChildObject.parentProp
Now when you want the property..