I'm trying to access and modify data that is in a parent class which is a child of another class. I've a parent class
class GrandParent {
protected $data = 1;
public function __construct() {}
public function getData() {
return $this->data;
}
}
Below is my first level child
class Child extends GrandParent {
protected $c1Data;
public function __construct() {
$this->c1Data = parent::getData();
$this->c1Data = 2;
}
public function getData() {
return $this->c1Data;
}
}
If I try to instantiate the Child class and do getData(), I get 2 which is normal. I've another class that inherits Child.
class GrandChild extends Child {
protected $c2Data;
public function __construct() {
$this->c2Data = parent::getData();
}
public function getData() {
return $this->c2Data;
}
}
The problem is that if I try to instantiate GrandChild I and get the data I'm getting null. Is it possible to make my GrandChild class inherit $c1Data = 2 and work with it. I want also to be able to use the Child and GrandParent classes on their own and not be abstract.