클래스 'base'와 클래스 'loader'가 있습니다. __get 및 __set 마법 메서드에 액세스 할 수 없습니다.
class base {
protected $attributes = Array();
public $load = null;
function __construct() {
$this->load = loader::getInstance();
echo $this->load->welcome(); //prints Welcome foo
echo $this->load->name; //prints Foo
echo $this->name; //doesnt print anything and i want it to print Foo
}
public function __get($key) {
return array_key_exists($key, $this->attributes) ? $this->attributes[$key] : null;
}
public function __set($key, $value) {
$this->attributes[$key] = $value;
}
}
class loader {
private static $m_pInstance;
private function __construct() {
$this->name = "Foo";
}
public static function getInstance() {
if (!self::$m_pInstance) {
self::$m_pInstance = new loader();
}
return self::$m_pInstance;
}
function welcome() {
return "welcome Foo";
}
}
$b = new base();
지금 내가 원하는 로더 클래스의 변수를 저장하고
$this->variablename
를 사용하여 기본 클래스에서 액세스 할 수있는 방법이다.
어떻게하면됩니까? extends
을 사용하고 싶지 않습니다. 어떤 생각?
'loader' 클래스 인스턴스는'base'의 생성자를 통해 전달되어야합니다. –
나는 이것을 전달하고 기본 클래스 변수 'load'에 $ this-> load = loader :: getInstance();를 사용하여 로더 클래스 인스턴스를 저장하고있다. –
그건 다르지 않습니다. 생성자를 통해 전달하는 것이 더 깔끔합니다. –