2012-12-18 16 views
0

我正在嘗試使用PHPStorm在PHP中編寫一些抽象的MVC類。我用於泛型類包裝器的一些重寫類屬性往往更適合使用靜態方法和屬性。 代碼工作正常,但每當我用一個變量來表示一個類名稱時,將它指向一個靜態變量(存在於繼承方法中的一個變量)時,PHPStorm不知道如何處理它並在IDE中將其顯示爲錯誤。我只是想知道是否有更好的方法來做這種事情,或者如果它只是我必須學會在IDE中忽略的東西。使用變量來表示PHPStorm中的靜態屬性的類名稱

例子:

class AbstractModel { 
    protected static $_dataMapper  = null; 
    protected static $_subs = null; 

    public function __construct($data=null) { 
     // constructor omitted with things like a generic class init, etc. 
    } 

    // $_subs is a static array who's value is a class name use for 
    // populating sub-objects as part of a class instance 
    public function setSubProperty($subKeyName,$value) { 
     $dataType = get_called_class(); 

     /************************************* 
     * PHPStorm complains here on $_subs * 
     *************************************/ 
     if(is_array($dataType::$_subs) 
      && array_key_exists($subKeyName,$dataType::$_subs)) { 

      $setMethod = 'set' . ucfirst($subKeyName); 
      return $dataType->$setMethod($value); 
     } else { 
      return false; // or throw an exception 
     } 
    } 
} 

class SomedataModel extends AbstractModel { 
    public $other = null; 
    protected static $_subs = array(
     'other' => "OtherModel" 
    ); 

    public function __construct($data=null) { 
     parent::__construct($data=null); 
     foreach(array_keys(self::$_subs) as $_k) { 
      $setMethod = 'set'.ucfirst($_k); 
      $this->$setMethod(); 
     } 
    } 

    public function setOther($data=null) { 
     // sanitize and set instance of 'other' as $this->other 
    } 
} 

回答

1

您可以輕鬆解決,通過使用static關鍵字來代替:

... 

public function setSubProperty($subKeyName,$value) { 

    if (is_array(static::$_subs) 
     && array_key_exists($subKeyName, static::$_subs)) { 

     $setMethod = 'set' . ucfirst($subKeyName); 

     ... 

PHPStorm支持非常好。不支持將變量字符串值作爲類名解析靜態成員和屬性。你可能想打開一個功能請求(如果它還不存在),但是我懷疑它在技術上是可行的,因爲它不是類型提示,而是值得提示,我認爲它在Phpstorm中不被支持。