解决方案:
谷歌了一通,发现Yii2的核心开发人员之一,亚历山大,已经给出了解决方案,GITHub链接原理就是重新定义一个Yii类,然后利用PhpDoc,如:
/**
* 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就可以去对应的类里找到相应的方法,然后提示代码了。
大部分PHP框架中,为了防止一个类被重复实例化,往往采用“单例模式”实例化类。我们的项目框架是这样做的:先写好一个基类 /framework/Base.class.php,内容如下:
12345678910111213141516171819202122232425262728293031323334353637
<?phpnamespace frameworkdefined('SITE_PATH') or die('Access Denied')/** * 业务基类 * * @package framework */class Base{/** * 类实例化(单例模式) * * @return mixed * @throws \Exception */public static function instance(){static $_instance = array()$classFullName = get_called_class() if (!isset($_instance[$classFullName])){core_load_class($classFullName) if (!class_exists($classFullName, false)){throw new \Exception('"' . $classFullName . '" was not found !') }$_instance[$classFullName] = new $classFullName() } return $_instance[$classFullName] }}?>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)