B
B
bedolazhka2019-11-26 05:15:30
PHP
bedolazhka, 2019-11-26 05:15:30

Why is the property for the child class model not populated?

abstract class BaseModel
{
    public $a;
}

class ModelA extends BaseModel
{
    public function __construct()
    {
        $this->a = 'Тест';
    }
}

class ModelB extends BaseModel
{
    public function __construct()
    {
    var_dump($this->a); // null
    }
}

abstract class ControllerA
{
    private $mObj;

    public function __construct()
    {
        $this->mObj = new ModelA();
    }
}

class ControllerB extends ControllerA
{
    public function __construct()
    {
        $mObj = new ModelB();
        var_dump($mObj->a); // null
    }
}
new ControllerB; // null

That is:
- we call ControllerB at the same time as the parent of ControllerA
- in ControllerA we call ModelA, which fills in property a for its parent - BaseModel
- in ControllerB we call ModelB, which receives property a from its parent - BaseModel
NULL
What am I not seeing?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DevMan, 2019-11-26
@bedolazhka

offhand:

class ModelB extends ModelA
{
    public function __construct() {
    parent::__construct();
    var_dump($this->a);
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question