类中存在PHP属性


PHP Property exists in class

嘿,伙计们,我遇到了一个奇怪的问题,$this没有在我的代码中传递,我希望有人可以帮我,结构如下:

Class 
{
    protected _foo = '';
    .........

    self::_setSessionsToProperties('_foo', array('access_token','instance_url'));
    ..........
    protected static function _setSessionsToProperties($property, $setter)
    {
        self::_validateApprovedSessions($setter);
        if (isset($this->$property) || property_exists($this, $property)) 
        {
            foreach ($setter as $set) { $this->$property->$set; }
        }
        return $this;
    }

Undefined variable: this

为了实现这一点,我实际上必须$this作为参数字符串传递似乎极不符合直觉?

    self::_setSessionsToProperties($this, '_foo', array('access_token','instance_url'));
    ..........
    protected static function _setSessionsToProperties($this, $property, $setter)

这是怎么回事??

$this仅存在于对象上下文中。由于类方法是static,因此没有对象。因此没有$this。使方法不是静态的,并实例化一个对象。

另请参阅如何使用Statics不破坏您的可测试性。

在几乎所有OO语言中,静态方法都存在于$this范围之外,这就是为什么可以在不初始化类实例的情况下调用它们。您必须传递它,或者将它作为静态变量存储在其他地方才能引用它。