0

Inheritance of properties is possible when a property is hardcoded. See below:

class ParentObj {   

    protected $familyName = 'Lincoln';   

}   

class ChildObj extends ParentObj {   

    public function __construct() {   
        var_dump($this->familyName);  
    }   
}   

$childObj = new ChildObj();   

// OUTPUT 
string 'Lincoln'

Inheritance of properties is not possible when a property is dynamic. See below:

class ParentObj {   

    protected $familyName; 

    public function setFamilyName($familyName){  
        $this->familyName = $familyName;  
    } 

}   

class ChildObj extends ParentObj {   

    public function __construct() {   
        var_dump($this->familyName);  
    }   
}   

$familyName = 'Lincoln';  
$parentObj = new ParentObj();  
$parentObj->setFamilyName($familyName); 

$childObj = new ChildObj(); 

// OUTPUT
null

So the question is: Why is not possible for a child class to inherit properties class that are set dynamically?

Julian
  • 4,396
  • 5
  • 39
  • 51

2 Answers2

3

The child inherits it's initial state from the parent class. It does not inherit from the concrete parent object instance.

In your first example, "Lincoln" is applicable to all ParentObject instances created. In your second example, it is applicable to the concrete $parentObj only. You are setting it specifically to that instance.

See my answer What is a class in PHP? for a more thorough explanation.

Gordon
  • 312,688
  • 75
  • 539
  • 559
  • Short but sweet, can you also tell me if it's possible to inherit someway from the concrete parent object instance? – Julian Jan 10 '20 at 14:26
  • 2
    @Julian you could make it static as shown elsewhere on the page, but that would then make it the value of each and every child you create as well as changing it for every existing child instance. If that's not what you want, you could have a method on the parent that creates children and then sets it's own state to the child. Then you'd do something like `$parentObj->createChild();`. – Gordon Jan 10 '20 at 14:37
1

If you wanted to access the value of $familyName from all instances(objects), you can define $familyName as static, i.e. create a global class variable.

e.g.

<?php

class ParentObj {

    protected static $familyName;

    public function setFamilyName($familyName){
        self::$familyName = $familyName;
    }

}

class ChildObj extends ParentObj {

    public function __construct() {
        var_dump(self::$familyName);
    }
}

$familyName = 'Lincoln';
$parentObj = new ParentObj();
$parentObj->setFamilyName($familyName);
$childObj = new ChildObj(); // Output: Lincoln

$familyName = 'Lee';
$parentObj->setFamilyName($familyName);
$childObj = new ChildObj(); // Output: Lee

Note of Caution: $familyName is now a global and will change for all instances of this object. This could lead to unexpected results if you ever change the value within the script. Global variables are generally considered a Bad Idea.

jibsteroos
  • 1,366
  • 2
  • 7
  • 13
  • 1
    Just remember that `$familyName` is now a global and will change for all instances of this object. This could lead to unexpected results if you ever change the value within the script. Global variables are generally considered a Bad Idea. – Tim Morton Jan 10 '20 at 14:41
  • @jibsteroos So a static property is a way to achieve shared memory? – Julian Jan 13 '20 at 09:33
  • Basically, static properties are useful for storing a persistent value relevant to a particular class, one that is not owned by instances of the class (objects). A way to look at them is as [global variables](https://www.php.net/manual/en/language.variables.scope.php) for classes. You thus don't have to create an instance of the class (an object) to access the property. For more on static properties, please have a look at [this](https://www.php.net/manual/en/language.oop5.static.php) – jibsteroos Jan 13 '20 at 10:02