我的道歉,如果我的標題是不明確的,請讓我解釋...PHP:類間傳遞動態變量
我有一個模板類呼叫「頁」,當我初始化它,我可以將它設置成通過設置$模式進入調試模式。
問題是我也有一個數據庫類,我想分享一個事實,即'頁面'處於調試模式,但不知道最好的辦法做到這一點......我不能有'頁面'擴展'db',沒有'page'實際調用'db'類的地方。它始終在其他課程上發生。
再次,對不起,如果你不清楚這個......我試圖提供的代碼,以幫助的一個非常簡單的版本...
class page {
public $mode; // what mode the page is in (debug/normal)
public function __construct($constructVars) {
$this->mode = $constructVars["mode"]; //debug
}
public function debug($message){
if ($this->mode == "debug"){
$fh = fopen("debug.txt", 'a') or die("can't open debug file");
fwrite($fh, date("Y-m-d H:i:s")." : ".$message."\n");
fclose($fh);
}
}
}
class db extends PDO {
public function debugOutput() {
$page = new page();
if ($page->mode == "debug"){
$fh = fopen("debug.txt", 'a') or die("can't open debug file");
fwrite($fh, $message."\n");
fclose($fh);
}
}
}
只是重申,因爲我認爲這是要去答案來了,我不能擴大頁面,包括數據庫。
份額的事實,它在調試模式與誰? – Brad 2013-03-21 23:42:26
實際代碼有什麼問題? – 2013-03-21 23:46:17
您正在訪問db類中的頁面類中的mode變量。你在哪裏難以獲得模式? – 2013-03-21 23:58:21