Heim >Backend-Entwicklung >PHP-Tutorial >yii 2 相比yii 1.1.x 有什么变化?
yii 2 相比yii 1.1.x ,有什么优点和变化 ?选择时应该注意什么?
In this chapter, we list the major changes introduced in Yii 2.0 since version 1.1. We hope this list will make it easier for you to upgrade from Yii 1.1 and quickly master Yii 2.0 based on your existing Yii knowledge.
本章将列出自1.1版本以来Yii 2.0的主要变化。
The most obvious change in Yii 2.0 is the use of namespaces. Almost every core class is namespaced, e.g., yii\web\Request. The “C” prefix is no longer used in class names. The naming of the namespaces follows the directory structure. For example, yii\web\Request indicates the corresponding class file is web/Request.php under the Yii framework folder. You can use any core class without explicitly including that class file, thanks to the Yii class loader.
Yii 2.0最明显的改变是对命名空间的使用。几乎所有的核心类都使用了命名空间,比如yii\web\Request。同时,类名前不再使用“C”前缀。命名空间的命名遵循目录结构,如yii\web\Request代表的相应类文件是位于Yii 框架的目录下的 web/Request.php。由于Yii的类装载机制,可以在未显示包含类文件的情况下使用任意的核心类。
Yii 2.0 breaks the CComponent class in 1.1 into two classes: [[yii\base\Object]] and [[yii\base\Component]]. The [[yii\base\Object|Object]] class is a lightweight base class that allows defining class properties via getters and setters. The [[yii\base\Component|Component]] class extends from [[yii\base\Object|Object]] and supports the event feature and the behavior feature.
Yii 2.0将1.1版本中的CComponent类拆分成两个类:[[yii\base\Object]] 和 [[yii\base\Component]]。其中,[[yii\base\Object|Object]]类是一个轻量级的基类,它通过getter 和setter提供了定义类属性(property)的方法。[[yii\base\Component|Component]]继承自[[yii \base\Object|Object]],并提供对事件(event)和行为(behavior)的支持。
If your class does not need the event or behavior feature, you should consider using Object as the base class. This is usually the case for classes that represent basic data structures.
如果自定义类不需要事件或行为特性,可考虑使用Object 作为基类。这通常用于表示基础的数据结构。
The [[yii\base\Object|Object]] class introduces a uniform way of configuring objects. Any descendant class of [[yii\base\Object|Object]] should declare its constructor (if needed) in the following way so that it can be properly configured:
[[yii\base\Object|Object]]对配置对象引入了一个规范的方法。其后代类可以在需要的情况下通过如下的方式声明一个构造函数,那么该类就可以被正确的配置:
<code class="language-php"><span class="x">class MyClass extends \yii\base\Object</span>
<span class="x">{</span>
<span class="x"> function __construct($param1, $param2, $config = [])</span>
<span class="x"> {</span>
<span class="x"> // ... initialization before configuration is applied</span>
<span class="x"> parent::__construct($config);</span>
<span class="x"> }</span>
<span class="x"> public function init ()</span>
<span class="x"> {</span>
<span class="x"> parent::init();</span>
<span class="x"> // ... initialization after configuration is applied</span>
<span class="x"> }</span>
<span class="x">}</span>
</code>
门外汉开个头(用过一小段时间1以及看2发布了看了点核码)