yii2框架随笔6
现在我们来揭开YII2最基础的类Object.php类这神秘的面纱(他是所有类的老祖宗)!
目录位置是base/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; * } * * public function setLabel($value) * { * $this->_label = $value; * } * ~~~ * * 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, * 属性能够被当做对象的成员变量使用 * * ~~~ * // 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. * * One can call [[hasProperty()]], [[canGetProperty()]] and/or [[canSetProperty()]] to check the existence of a property. * * 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. * * In the above, both Step 2 and 3 occur at the end of the class constructor. It is recommended that * you perform object initialization in the `init()` method because at that stage, the object configuration * is already applied. * * 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. * * @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()]]. *构造函数,默认的实现做了两件事
*初始化给定的配置$config.
*使用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 = []) { 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. */ 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)) { return $this->$getter(); } elseif (method_exists($this, 'set' . $name)) { throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name); } else { // 否则认为该属性不存在 throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name); } } /** * Sets 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 `$object->property = $value;`. * * 魔术方法,实现 setter * * @param string $name the property name or the event name * @param mixed $value the property value * @throws UnknownPropertyException if the property is not defined * @throws InvalidCallException if the property is read-only * @see __get() */ public function __set($name, $value) { $setter = 'set' . $name; if (method_exists($this, $setter)) { // 对象存在 $setter 方法,就直接调用 $this->$setter($value); } elseif (method_exists($this, 'get' . $name)) { throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name); } else { throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name); } }
/** * Checks if the named property is set (not null). * * Do not call this method directly as it is a PHP magic method that * will be implicitly called when executing `isset($object->property)`. * * Note that if the property is not defined, false will be returned. * * 魔术方法,实现 isset,基于 getter 实现,有 getter 方法的属性才算存在 * * @param string $name the property name or the event name * @return boolean whether the named property is set (not null). */ public function __isset($name) { $getter = 'get' . $name; if (method_exists($this, $getter)) { // 有 $getter 方法且获取的值不为 null,才认为该属性存在 return $this->$getter() !== null; } else { return false; } }
/** * Sets an object property to null. * * Do not call this method directly as it is a PHP magic method that * will be implicitly called when executing `unset($object->property)`. * * Note that if the property is not defined, this method will do nothing. * If the property is read-only, it will throw an exception. * * 魔术方法,实现 unset,基于 setter 实现,有 setter 方法的属性才能 unset 掉 * * @param string $name the property name * @throws InvalidCallException if the property is read only. */ public function __unset($name) { $setter = 'set' . $name; if (method_exists($this, $setter)) { // 通过 $setter 方法,将它设置为 null $this->$setter(null); } elseif (method_exists($this, 'get' . $name)) { // 如果存在 'get' . $name 方法,就认为该属性是只读的 throw new InvalidCallException('Unsetting read-only property: ' . get_class($this) . '::' . $name); } }