Вам нужно построить родителя. То, что дочерний класс расширяет родительский класс, не означает, что родительский класс автоматически создается / создается, когда дочерний класс есть. Он просто наследует функциональность (свойства / методы).
Вы можете сделать это с: parent::__construct();
Я сделал несколько небольших правок для вашего источника, в частности PSR-2 в стиле имен классов и разрывов строк. Но все остальное то же самое.
<?php
class Test {
protected $testing = 'original';
function __construct(){
echo "The Test class has loaded (".$this->testing.")\n";
$this->testing = 'Test';
echo "Updated to (".$this->testing.")\n";
}
}
class TestTwo extends test {
function __construct(){
echo "Child class TestTwo class has loaded (".$this->testing.")\n";
parent::__construct();
echo "Parent class Test class has loaded (".$this->testing.")\n";
$this->testing = 'TestTwo';
echo "The string has been updated to (".$this->testing.")\n";
}
}
$test = new Test();
$testTwo = new TestTwo();
Что даст вам следующий вывод:
The Test class has loaded (original)
Updated to (Test)
Child class TestTwo class has loaded (original)
The Test class has loaded (original)
Updated to (Test)
Parent class Test class has loaded (Test)
The string has been updated to (TestTwo)