yii2框架随笔17
阅读object.php
<?php /** * @link http://www.yiiframework.com/ * @copyright Copyright (c) 2008 Yii Software LLC * @license http://www.yiiframework.com/license/ */ namespace yii\base; use Yii; /** * Object is the base class that implements the *property* feature. * Object 是一个基础类,实现了属性的功能 * * A property is defined by a getter method (e.g. `getLabel`), and/or a setter method (e.g. `setLabel`). For example, * the following getter and setter methods define a property named `label`: * 一个定义了 getter 方法和/或者 setter 方法的属性 * * ~~~ * private $_label; * * public function getLabel() * { * return $this->_label;通过getter方法取得_label的值 * } * * public function setLabel($value) * { * $this->_label = $value;通过setter方法设置_label的值 * } * ~~~ * * Property names are *case-insensitive*. * 属性名是大小写敏感的 * * A property can be accessed like a member variable of an object. Reading or writing a property will cause the invocation * of the corresponding getter or setter method. For example, * 属性能够像一个成员变量一样被访问,读、写将会调用对应的getter或者setter方法 * * ~~~ * // equivalent to $label = $object->getLabel(); * $label = $object->label; * // equivalent to $object->setLabel('abc'); * $object->label = 'abc'; * ~~~ * * If a property has only a getter method and has no setter method, it is considered as *read-only*. In this case, trying * to modify the property value will cause an exception. * 如果一个属性只有getter方法,那么这个属性是只读的,如果这样,试图修改这个属性的值,将会抛出异常. * One can call [[hasProperty()]], [[canGetProperty()]] and/or [[canSetProperty()]] to check the existence of a property. * 可以调用 [[hasProperty()]], [[canGetProperty()]] and/or [[canSetProperty()]] 检查一个属性是否存在。 * Besides the property feature, Object also introduces an important object initialization life cycle. In particular, * creating an new instance of Object or its derived class will involve the following life cycles sequentially: * 除了属性特征,还引入了一个重要的对象初始化。特别的,创建一个新实例的对象或其派生类将涉及以下顺序生命周期: * 1. the class constructor is invoked; * 调用类的构造函数; * 2. object properties are initialized according to the given configuration; * 根据给定的对象属性初始化配置; * 3. the `init()` method is invoked. * init()的调用方法。 * * In order to ensure the above life cycles, if a child class of Object needs to override the constructor, * it should be done like the following: * 为了确保上述的运转,如果一个子类的对象需要覆盖构造函数,它应该如下: * ~~~ * public function __construct($param1, $param2, ..., $config = []) * { * ... * parent::__construct($config); //构造函数。 * } * ~~~ * * That is, a `$config` parameter (defaults to `[]`) should be declared as the last parameter * of the constructor, and the parent implementation should be called at the end of the constructor. * * Yii最基础的类,大多数类都继承了该类 * * @author Qiang Xue <qiang.xue@gmail.com> * @since 2.0 */ class Object implements Configurable { /** * Returns the fully qualified name of this class. * 获取静态方法调用的类名。返回类的名称,如果不是在类中调用则返回 FALSE。 * * @return string the fully qualified name of this class. */ public static function className() { // get_called_class -- 后期静态绑定("Late Static Binding")类的名称 // 就是用那个类调用的这个方法,就返回那个类,返回值中带有 namespace return get_called_class(); } /** * Constructor. * The default implementation does two things: * 默认实现做两件事: * - Initializes the object with the given configuration `$config`. * - Call [[init()]]. * 构造方法实现了接口Configurable,通过传入的配置初始化对象,调用init()方法 * If this method is overridden in a child class, it is recommended that * * - the last parameter of the constructor is a configuration array, like `$config` here. * * - call the parent implementation at the end of the constructor. * 如果构造函数在子类中重写,必须调用父类的方法,且最后一个参数为配置数组 * @param array $config name-value pairs that will be used to initialize the object properties */ public function __construct($config = []) { // 根据 $config 内容初始化该对象 if (!empty($config)) { Yii::configure($this, $config); } // 调用 init() 方法,继承该类的类可以重写 init 方法,用于初始化 $this->init(); } /** * Initializes the object. * 初始化对象 * This method is invoked at the end of the constructor after the object is initialized with the * given configuration. * 在构造函数的末尾调用,可以重写 init 方法,用于初始化 */ public function init() { } /** * Returns the value of an object property. * 返回对象的属性值 * Do not call this method directly as it is a PHP magic method that * will be implicitly called when executing `$value = $object->property;`. * * 魔术方法,实现 getter * * @param string $name the property name * @return mixed the property value * @throws UnknownPropertyException if the property is not defined * @throws InvalidCallException if the property is write-only * @see __set() */ public function __get($name) { $getter = 'get' . $name; if (method_exists($this, $getter)) { // 对象存在 $getter 方法,就直接调用 return $this->$getter(); } elseif (method_exists($this, 'set' . $name)) { // 如果存在 'set' . $name 方法,就认为该属性是只写的 throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name); } else { // 否则认为该属性不存在 throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name); } }