原本是想读 /yii/di/Container 类但是发现,直接读这个类,还是有点不懂所以我就开始从它的继承的父级开始 。
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\base;
use Yii;
/**
* BaseObject是实现*property*特性的基类。
*
* 属性是由getter方法定义的(例如' getLabel ')和/或setter方法(例如“setLabel”)。例如,
* 以下getter和setter方法定义了一个名为' label '的属性:
*
* ```php
* private $_label;
*
* public function getLabel()
* {
* return $this->_label;
* }
*
* public function setLabel($value)
* {
* $this->_label = $value;
* }
* ```
*
* 属性名不区分大小写。
*
* 属性可以像对象的成员变量一样被访问。读取或写入属性将导致调用相应的getter或setter方法。例如,
*
* ```php
* // equivalent to $label = $object->getLabel();
* $label = $object->label;
* // equivalent to $object->setLabel('abc');
* $object->label = 'abc';
* ```
*
* 如果一个属性只有一个getter方法而没有setter方法,那么它被认为是“只读的”。在这种情况下,尝试修改属性值将导致异常。
*
* 可以调用 hasProperty() , canGetProperty() , canSetProperty() 来检查属性的存在
*
* 除了属性特性外,BaseObject还引入了一个重要的对象初始化生命周期.
* 特别地,创建一个新的BaseObject实例或它的派生类将依次涉及以下生命周期:
*
* 1. 调用类构造函数
* 2. 对象属性根据给定的配置初始化;
* 3. 调用 init() 方法
*
* 在上面的例子中,第2步和第3步都出现在类构造函数的末尾
* 建议您在' init() '方法中执行对象初始化,因为在这个阶段,对象配置已经被应用。
*
* 为了确保上述生命周期,如果BaseObject的一个子类需要重写构造函数,具体做法如下:
*
* ```php
* public function __construct($param1, $param2, ..., $config = [])
* {
* ...
* parent::__construct($config);
* }
* ```
*
* 也就是说,应该将' $config '参数(默认值为'[]')声明为构造函数的最后一个参数,并在构造函数的末尾调用父实现。
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0.13
*/
class BaseObject implements Configurable
{
/**
* 返回该类的完全限定名。
* @return string 该类的完全限定名。
* @deprecated since 2.0.14. 在PHP >=5.5中,使用'::class '代替。
*/
public static function className()
{
return get_called_class();
}
/**
* 构造函数
*
* 默认的实现做两件事:
*
* - 使用给定的配置' $config '初始化对象。
* - 调用 init()
*
* 如果在子类中重写此方法,建议这样做
*
* - 构造函数的最后一个参数是一个配置数组,如这里的' $config '。
* - 在构造函数的末尾调用父实现。
*
* @param array $config 将用于初始化对象属性的名称-值对
*/
public function __construct($config = [])
{
if (!empty($config)) {
Yii::configure($this, $config);
}
$this->init();
}
/**
* 初始化对象。
* 使用给定的配置初始化对象之后,在构造函数的末尾调用此方法。
*/
public function init()
{
}
/**
* 返回对象属性的值。
*
* 不要直接调用这个方法,因为它是一个PHP魔法方法,在执行' $value = $object->property; '时会隐式地调用它。
*
* @param string $name 属性名
* @return mixed 属性值
* @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);
}
throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
}
/**
* 设置对象属性的值
*
* 不要直接调用这个方法,因为它是一个PHP魔法方法
* 将在执行“$object->property = $value;”时隐式调用。
*
* @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)) {
$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);
}
}
/**
* 检查属性是否已设置,即是否已定义,是否不为空。
*
* 不要直接调用这个方法,因为它是一个PHP魔法方法
* 将在执行“isset($object->属性)”时隐式调用。
*
* 注意,如果属性没有定义,将返回false。
*
* @param string $name the property name or the event name
* @return bool whether the named property is set (not null).
* @see https://secure.php.net/manual/en/function.isset.php
*/
public function __isset($name)
{
$getter = 'get' . $name;
if (method_exists($this, $getter)) {
return $this->$getter() !== null;
}
return false;
}
/**
* 将对象属性设置为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.
* @param string $name the property name
* @throws InvalidCallException if the property is read only.
* @see https://secure.php.net/manual/en/function.unset.php
*/
public function __unset($name)
{
$setter = 'set' . $name;
if (method_exists($this, $setter)) {
$this->$setter(null);
} elseif (method_exists($this, 'get' . $name)) {
throw new InvalidCallException('Unsetting read-only property: ' . get_class($this) . '::' . $name);
}
}
/**
* 调用命名的方法,而不是类方法。
*
* Do not call this method directly as it is a PHP magic method that
* will be implicitly called when an unknown method is being invoked.
* @param string $name the method name
* @param array $params method parameters
* @throws UnknownMethodException when calling unknown method
* @return mixed the method return value
*/
public function __call($name, $params)
{
throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
}
/**
* 返回一个值,该值指示是否定义了属性
*
* 属性定义如下:
*
* - 该类具有与指定名称相关联的getter或setter方法(在本例中,属性名不区分大小写);
* - 该类有一个具有指定名称的成员变量(当' $checkVars '为真时);
*
* @param string $name the property name
* @param bool $checkVars whether to treat member variables as properties
* @return bool whether the property is defined
* @see canGetProperty()
* @see canSetProperty()
*/
public function hasProperty($name, $checkVars = true)
{
return $this->canGetProperty($name, $checkVars) || $this->canSetProperty($name, false);
}
/**
* 返回一个值,该值指示是否可以读取属性
*
* 属性是可读的,如果:
*
* - 该类具有与指定名称相关联的getter方法(在本例中,属性名不区分大小写);
* - 该类有一个具有指定名称的成员变量(当' $checkVars '为真时)
*
* @param string $name the property name
* @param bool $checkVars whether to treat member variables as properties
* @return bool whether the property can be read
* @see canSetProperty()
*/
public function canGetProperty($name, $checkVars = true)
{
return method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name);
}
/**
* 返回一个值,该值指示是否可以设置属性
*
* A property is writable if:
*
* - the class has a setter method associated with the specified name
* (in this case, property name is case-insensitive);
* - the class has a member variable with the specified name (when `$checkVars` is true);
*
* @param string $name the property name
* @param bool $checkVars whether to treat member variables as properties
* @return bool whether the property can be written
* @see canGetProperty()
*/
public function canSetProperty($name, $checkVars = true)
{
return method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name);
}
/**
* 返回一个值,该值指示是否定义了一个方法。
*
* The default implementation is a call to php function `method_exists()`.
* You may override this method when you implemented the php magic method `__call()`.
* @param string $name the method name
* @return bool whether the method is defined
*/
public function hasMethod($name)
{
return method_exists($this, $name);
}
}