這應該很容易..有人可以解釋我的語法嗎?PHP在父類中訪問類對象
我有一個控制器實例化一個引導類(新關鍵字),它實例化配置類。
然後控制器實例化一個擴展引導類的起始頁類。在startpage類中,我試圖訪問bootstrap(父類)類中的配置對象。
這可以完成嗎?或者startpage是否必須直接實例化bootstrap?是否實例化擴展引導程序的首頁,覆蓋引導程序?或者我的語法錯了?
控制器(索引頁)
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 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>';
}
}
}
首頁中國類:
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();
}
}
'parrent :: obj' .. – Davit
你是否看到'配置類缺失'異常? – hohner
試過。 ** $ charset = parent :: $ config-> getCharset(); ** ...同樣的錯誤。 – obmon