2017-04-11 197 views
0

在其他類中,PhpStorm可以識別__construct()函數,但是在yaf控制器中它不能識別初始化方法init(),導致init()無法跟蹤初始化操作。PhpStorm不支持​​yaf的init方法

class TestController extends Yaf_Controller_Abstract{ 
    private $model; 
    public function init() { 
     $this->model = new TestModel(); 
    } 

    public function test(){ 
     $this->model->testDeclaration(); 
    } 
} 

class TestModel{ 
    public function testDeclaration(){ 
    } 
} 

在這個例子中,我想在TestModel類使用「go to declaration」自測試功能$this->model->testDeclaration();testDeclaration()功能。但它不起作用。

PhpStorm告訴我:

找不到聲明去

如何使用 '去申報' 正確嗎?

回答

1

在其它類,PhpStorm可以識別__construct()功能,但在YAF控制器它無法識別初始化方法init(),導致init()無法跟蹤初始化操作。

PhpStorm對__constructor()有特殊處理 - 它跟蹤哪些類型的變量/屬性會在方法體內進行任何賦值操作。

例如,在此代碼中,它知道$this->model將是TestModel類的實例 - IDE將此信息保存在__construct()方法主體之外。

對於其他方法,如init()在你的情況下,這些信息會被丟棄到外面(所以它只在方法體的本地)。


您可以輕鬆地通過使用簡單的PHPDoc的評論與@var標籤,你會爲model屬性提供類型提示解決此問題:

/** @var \TestModel Optional description here */ 
private $model; 

讓所有屬性/類變量提供類型暗示的習慣即使IDE自動檢測其'類型 - 它可以長期幫助IDE。

https://phpdoc.org/docs/latest/references/phpdoc/tags/var.html

+0

非常感謝你,這樣可以 –

+0

@張軒銘是否能解決您的問題,即考慮將其標記這個答案所接受。 – LazyOne