I have a very simple OO class structure and cannot get my head around why the child class is not inheriting the properties and methods from the parent.
This is a basic example of my set up:
//Main class:
class Main{
//construct
public function Main(){
//get data from model
$data = $model->getData();
//Get the view
$view = new View();
//Init view
$view->init( $data );
//Get html
$view->getHTML();
}
}
//Parent View class
class View{
public $data, $img_cache;
public function init( $data ){
$this->data = $data;
$this->img_cache = new ImageCache();
}
public function getHTML(){
//At this point all data is intact (data, img_cache)
$view = new ChildView();
//After getting reference to child class all data is null
//I expected it to return a reference to the child class and be able to
//call the parent methods and properties using this object.
return $view->html();
}
}
//Child View Class
class ChildView{
public function html(){
//I get a fatal error here: calling img_cache on a non-object.
//But it should have inherited this from the parent class surely?
return '<img src="'.$this->img_cache->thumb($this->data['img-src']).'"/>';
}
}
So I expected the child class to inherit the properties and methods from the parent. Then when I get a reference to the child class it should be able to use the img_cache
object. But I get a fatal error here: Call to a member function thumb() on a non-object
.
Where have I gone wrong with this?