1
我創建了一個名爲Boot
的類,在這裏面我改變了文件的路徑,所以用戶可以調用它來設置自定義路徑,如下所示:無法訪問更改的屬性
class Boot
{
private static $_filePath = 'directory/';
public function __construct()
{
require 'system.php';
}
public function init()
{
new System();
}
public function setFilePath($newDir)
{
$this->_filePath = $newDir;
}
public static function getFilePath()
{
return self::_filePath;
}
}
所以在我index.php
文件:
require 'boot.php';
$b = new Boot();
$b->setFilePath('directories/');
$b->init();
系統類
現在我把這樣的事情:
echo Boot::getFilePath();
並應顯示directories/
但我再次看到默認值:directory
。
現在我雖然認爲這個問題涉及到static
這個字段,但是我怎樣才能訪問到更改後的值呢?謝謝。
nope,我在'System'中沒有'Boot'的任何實例。我在'index.php'文件中有'Boot'的實例,但是我需要從'boot'中聲明的類'system'中訪問'boot'內'$ _filePath'的路徑。我不知道現在是否更清楚。 –
將'$ b'作爲參數傳遞給'System'構造函數。 –
這是解決問題的唯一解決方案嗎? –