This should be easy.. can someone explain the syntax for me?
I have a controller which instantiates a bootstrap class (new keyword) which instantiates the config class.
the controller then instantiates a startpage class which extends the bootstrap class. In the startpage class I’m trying to access the config object in the bootstrap (parent) class.
Can this even be done? Or does startpage have to instantiate bootstrap directly? Does instantiating startpage that extends bootstrap, overwrite bootstrap? or is my syntax just wrong?
Controller (index page)
try {
if (!include($paths['root'] . $paths['framework'] . '/core/AutoLoader.php')) {
throw new Exception ('<b>Error - AutoLoader is missing</b>');
}
$loader = new AutoLoader($paths);
$appStack = new BootStrap($paths);
$app = new StartPage();
$app->start();
} catch (Exception $e) {
echo
'<p><b>EXCEPTION</b><br />Message: '
. $e->getMessage()
. '<br />File: '
. $e->getFile()
. '<br />Line: '
. $e->getLine()
. '</p>';
}
Bootstrap class:
class BootStrap {
protected $config;
/**
* --------------------------------------------------------------------------
** GETTERS
* --------------------------------------------------------------------------
*
*/
public function getConfig() { return $this->config; }
/**
* --------------------------------------------------------------------------
* __construct()
* PUBLIC method
* = Starts a new session, loads stylesheets, loads classes
* --------------------------------------------------------------------------
*
*/
public function __construct($paths) {
/**
* --------------------------------------------------------------------------
* load Config class
* --------------------------------------------------------------------------
*
*/
try {
if (!class_exists('Config')) {
throw new Exception ('<b>Error - Configuration class is missing</b>');
}
$this->config = new Config();
} catch (Exception $e) {
echo
'<p><b>EXCEPTION</b><br />Message: '
. $e->getMessage()
. '<br />File: '
. $e->getFile()
. '<br />Line: '
. $e->getLine()
. '</p>';
}
}
}
Startpage class:
class StartPage extends BootStrap {
/**
* --------------------------------------------------------------------------
* __construct()
* PUBLIC method
* = Starts a new session, loads stylesheets, loads classes
* --------------------------------------------------------------------------
*
*/
public function __construct() {
}
/**
* --------------------------------------------------------------------------
* Start()
* PUBLIC method
* = loads the web page
* --------------------------------------------------------------------------
*
*/
public function Start() {
// path to includes
$inc_path = $this->paths['root'] . $this->paths['medium'];
// instantiate page, html header
$charset = $this->config->getCharset();
$title = $this->config->getTitle();
$description = $this->config->getDescription();
}
}
As soon as you give
StartPageits own__construct()method, you effectively “hide” the one inBootstrap, over-riding it completely. So$foo = new StartPage()only runs the__construct()code from theStartPageclass.In order to also run the
__construct()code in theBootstrapparent class, you have to add an explicit call to it, usingparent::__construct().In case you wonder why PHP doesn’t do this for you, here are three things you couldn’t do if it were automatic:
parent::__construct().EDIT: To summarise the further clarifications below, creating a particular instance of the parent (
Bootstrap) class will not make any difference to later creations of other instances of the same class or its child class (StartPage); each is a separate object and will call__constructindependently.If you want multiple
StartPageobjects to refer to one particular instance of theBootstrapclass, then inheritance is not the right mechanism. Instead, you will need to pass the createdBootstrapinstance into eachStartPageinstance via some form of Dependency Injection.