2013-02-21 35 views
1

嗨我在与$this没有在我的代码中传递的一个奇怪的问题,我希望 有人能帮助我,结构如下:PHP属性存在于类

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) 

这是怎么回事?

+0

您不能在静态函数中使用'$ this'。 – 2013-02-21 20:41:02

回答

1

几乎所有OO语言都存在$ this范围之外的静态方法,这就是为什么可以调用它们而无需启动该类的一个实例。你要么必须传递它,要么将其作为一个静态变量存储在其他地方,以便能够引用它。