Yii2 Day 3: 讓PHPStorm支持自定義Component依賴注入代碼提示

場景是這樣的:php

定義了一個 MyComponent,在配置文件中加上這個組件的定義,好比: 'myComponent'=>['class'=>'app/components/MyComponent'], 就能夠用Yii::$app->myComponent調用啦,可是,當輸入、Yii::$app-> 後,myComponent不在自動提示的列表裏,而且這個MyComponent的各類方法,PHPStorm沒法給出代碼提示,這對於追求效率和準確性的程序員是不可忍受的。git

解決方案:程序員

谷歌了一通,發現Yii2的核心開發人員之一,亞歷山大,已經給出瞭解決方案,GITHub連接github

原理就是從新定義一個Yii類,而後利用PhpDoc,如:yii2

<!-- lang: php -->
/**
 * Class BaseApplication
 * Used for properties that are identical for both WebApplication and ConsoleApplication
 *
 * @property \app\components\MyComponent $myComponent
 * @property \app\components\Mailer $mailer The mailer component. This property is read-only. Extended component.
 */
abstract class BaseApplication extends yii\base\Application
{
}

有了@property定義後,PHPStorm就能夠去對應的類裏找到相應的方法,而後提示代碼了。app

相關文章
相關標籤/搜索