简体   繁体   English

__get和__set魔术方法不可访问

[英]__get and __set magic methods not accessible

I have a class 'base' and a class 'loader', which looks like this. 我有一个类'base'和一个类'loader',看起来像这样。

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();

Now what I want is a way to store variables from loader class and access them from base class using $this->variablename . 现在我想要的是一种存储来自加载器类的变量,并使用$this->variablename从基类访问$this->variablename

How can I achieve this? 我该如何实现? I don't want to use extends . 我不想使用extends Any idea ? 任何想法 ?

I don't feel like you've fully understood what coding the OOP way means. 我觉得您不完全了解OOP方式编码的含义。 And usually Singletons are code smells so I'll just warn you: 通常Singletons都是代码气味,因此我只警告您:

There's probably a better way of accomplish you goal. 可能有更好的方法可以达成目标。 If you provide more informations we will help you out. 如果您提供更多信息,我们将为您提供帮助。 In its current form the answer is the following; 按照目前的形式,答案如下: just remember that I higly discourage its implementation in your code. 只要记住,我在您的代码中强烈建议不要执行它。

Assuming that you want to access only public (and non static) loader 's variables as this->varname in the base class you should just insert this line in the beginning of the base class constructor: 假设您只想在base类中以this->varname方式访问公共(和非静态) loader变量,则只需在基类构造函数的开头插入以下行:

$this->attributes = get_object_vars(loader::getInstance());

This will basically initialize the attributes array with all the loader public vars so that via your __get() method you can access its value. 基本上,这将使用所有加载程序公共变量来初始化属性数组,以便通过__get()方法可以访问其值。

On a side note, take a look at Dependency Injection design pattern in order to avoid using Singletons. 另外,请避免依赖注入设计模式 ,以免使用Singletons。

Your __get/__set methods access $this->attributes but not $this->load . 您的__get / __ set方法访问$this->attributes但不能访问$this->load
You could eg do something like (pseudocode) 您可以例如执行类似(伪代码)的操作

function __get($key) {
  - if $attribute has an element $key->$value return $attribute[$key] else
  - if $load is an object having a property $key return $load->$key else
  - return null;
}

see also: http://docs.php.net/property_exists 另请参阅: http : //docs.php.net/property_exists

You can make static variable and then you can access this variable from anywhere 您可以创建静态变量,然后可以从任何地方访问此变量

public statis $var = NULL;

and you can access it like this 您可以像这样访问它

classname::$var;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM