туплю я чтото.
если передаю в конструктор родителя переменную
и эту переменную присваиваю свойству
получаю в родителе - переменную
в потомке - NULL
class Parent
{
public $child;
public function __construct()
{
$this->child = 'Child';
}
}
class Child extends Parent
{
public $other;
public function __construct()
{
parent::__construct();
$this->other = 'bla';
}
}
$obj = new Child();
echo $obj->child; // Child, not NULL
Если же ты хочешь, чтобы после $obj = Parent(), объект $obj был типа Child, то это бред, интацирование так не должно работать, если сильно надо, то юзай фабрику, к примеру:
class Parent
{
public $test;
public function __construct()
{
$this->test = 'test';
}
public static function factory($classSuffix = '')
{
$className = 'Parent' . ucfirst($classSuffix);
if (!class_exists($className))
{
throw new Exception("Class {$className} not exists");
}
$instance = new $className;
if (!($instance instanceof Parent))
{
throw new Exception("wtf?");
}
return $instance;
}
}
class ParentChild extends Parent
{
public $other;
public function __construct()
{
parent::__construct();
$this->other = 'other';
}
}
$p = Parent::factory();
$c = Parent::factory('Child');
echo "Parent::test = {$p->test}<br />ParentChild::test = {$c->test}<br />ParentChild::other = {$c->other};
Work, buy, consume, die