Base Class:
class A {
public x;
public y;
public function __construct {
$this->x = new X();
$this->y = new Y();
}
}
Class X:
class X extends A {
public function __construct {}
public function job() {
echo 'x working!';
}
}
Class Y:
class Y extends A {
public function __construct {}
public function job() {
var_dump($this->x); // NULL, Why???
$this->x->job();
}
}
Problem: When I’m calling x->job() from inside of class Y, I have no access to the X already instanced object totally, and var_dump shows it’s null.
Any ideas what’s wrong with that?
Thanks! 🙂
Update:
If I use parent::__construct(); in the child class’ __construct() method, then it would generates Fatal Error: maximum function nesting level of '100' reached, aborting!. That’s why I add those empty __construct() methods. Any idea again how to solve that?
I have this one also on the source code:
$base = new A();
So, the constructor should been run already, right?
You must call
parent::__construct()in the chil class constructor, otherwise the parent constructor is not called and the property won’t recieve its value.