I've created a Display object that contains the header, sidebar and footer:
class Display {
protected $framework;
public $mysql;
public function __construct() {
$this->mysql = new MySQL();
$this->framework .= $this->header();
$this->framework .= $this->body();
$this->framework .= $this->sidebar();
$this->framework .= $this->footer();
}
private function header (){ /* blah */ }
private function body (){ }
private function sidebar (){ /* blah */ }
private function footer (){ /* blah */ }
public function displayPage(){
print $this->framework;
}
}
On each page I've created a object that extends the Display object, with the code for the body:
class IndexPHP extends Display {
public function body(){
$this->user = new User();
return '<div class="body">Hello ' . $this->user->getName() . '</div>';
}
}
$page = new IndexPHP();
$page->displayPage();
Have I created a problem by nesting the objects too much? For example, in the User object, how do I access the already initiated MySQL object?
class User {
protected $name;
public function __construct() {
$this->id = /* MySQL object query here */
}
}