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'构造函数。 –
这是解决问题的唯一解决方案吗? –