Yii中实例化类的四种方式

1.最简单的也是最基本的,大家都会的一种

use yiicachingFileCache;
$cache = new FileCache();
$cache->set("name","value")

2.基于组件加载方式,将要实例化的类放在配置文件中的components配置中,视作一个组件进行加载

$cache = Yii::$app->cache; // 获取该对象

3.利用服务定位器(ServiceLocator)

use yiidiServiceLocator;
use yiicachingFileCache;
$locator = new ServiceLocator;
$locator->set('cache', 'yiicachingApcCache');

4.基于BaseYii类的核心方法之一的createObject静态方法

$cache = Yii::createObject([
    'class' => 'yiicachingFileCache',
]);
原文地址:https://www.cnblogs.com/adobe-lin/p/9333907.html