Yii源码阅读笔记(五)
Object 是一个基础类,实现了属性的功能,其基本内容如下:
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方法定义,如下面的例子
* ```php
* 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方法
* ```php
* // 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方法,那么这个属性是只读的,如果试图修改这个属性的值,将会抛出异常
* 能够调用 [[hasProperty()]], [[canGetProperty()]] and/or [[canSetProperty()]] 检查一个属性
*
* @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()
{
return get_called_class(); // get_called_class -- 后期静态绑定("Late Static Binding")类的名称
// 就是用那个类调用的这个方法,就返回那个类,返回值中带有 namespace
}
/**
* Constructor.
* The default implementation does two things:
* 构造方法实现了接口Configurable,通过传入的配置初始化对象,调用init()方法
* - Initializes the object with the given configuration `$config`.
* - Call [[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,在访问不存在的属性时调用,即成员变量设置为public时,不会被调用
*
* @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; //构造getter方法
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);
}
}
/**
* 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,在访问不存在的属性时调用,即成员变量设置为public时,不会被调用
*
* @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; //构造setter方法
if (method_exists($this, $setter)) {
// 对象存在 $setter 方法,就直接调用
$this->$setter($value);
} elseif (method_exists($this, 'get' . $name)) {
// 如果存在 'get' . $name 方法,就认为该属性是只读的,抛出异常
throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
} else {
// 否则认为该属性不存在,,抛出异常
throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
}
}
/**
* 检查属性是否被设置
*
* 魔术方法,实现 isset,用isset() 判断对象不可见的属性时(protected/private/不存在的属性)被调用
* 基于 getter 实现,有 getter 方法的属性才算存在
* @param string $name the property name or the event name
* @return boolean whether the named property is set (not null).
* @see http://php.net/manual/en/function.isset.php
*/
public function __isset($name)
{
$getter = 'get' . $name;
if (method_exists($this, $getter)) {
// 判断是否有getter方法,且是否有返回值,有 getter 方法且获取的值不为 null,才认为该属性存在
return $this->$getter() !== null;
} else {
return false;
}
}
/**
*
* 魔术方法,实现 unset,在注销不可见的属性时(protected/private/不存在的属性)时被调用
* 基于 setter 实现,有 setter 方法的属性才能 unset 掉
* @param string $name 属性名
* @throws InvalidCallException if the property is read only.
* @see http://php.net/manual/en/function.unset.php
*/
public function __unset($name)
{
$setter = 'set' . $name; //构造setter方法
if (method_exists($this, $setter)) {
// 如果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);
}
}
/**
*重写了__call()方法,在调用不存在的方法时会调用此方法,抛出异常
* @param string $name 方法名
* @param array $params 方法参数
* @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()");
}
/**
*
* 检查对象或类是否具有 $name 属性,如果 $checkVars 为 true,则不局限于是否有 getter/setter
*
* @param string $name 属性名
* @param boolean $checkVars 是否检查成员变量,默认为true
* @return boolean 属性是否被定义
* @see canGetProperty()
* @see canSetProperty()
*/
public function hasProperty($name, $checkVars = true)
{
return $this->canGetProperty($name, $checkVars) || $this->canSetProperty($name, false);
}
/**
*
* 检查对象或类是否能够获取 $name 属性,如果 $checkVars 为 true,则不局限于是否有 getter
*
* @param string $name 属性名
* @param boolean $checkVars 是否检查成员变量,默认为true
* @return boolean 属性是否可读
* @see canSetProperty()
*/
public function canGetProperty($name, $checkVars = true)
{
// property_exists — 检查对象或类是否具有该属性
return method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name);
}
/**
*
* 检查对象或类是否能够设置 $name 属性,如果 $checkVars 为 true,则不局限于是否有 setter
*
* @param string $name 属性名
* @param boolean $checkVars 是否检查成员变量,默认为true
* @return boolean 属性是否可写
* @see canGetProperty()
*/
public function canSetProperty($name, $checkVars = true)
{
return method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name);
}
/**
* 调用php的 `method_exists()`函数.
* You may override this method when you implemented the php magic method `__call()`.
*
* 检查对象或类是否具有 $name 方法
*
* @param string $name 方法名
* @return boolean 方法是否被定义
*/
public function hasMethod($name)
{
return method_exists($this, $name);
}
}
Yii源码阅读笔记(五)的更多相关文章
- Yii源码阅读笔记(一)
今天开始阅读yii2的源码,想深入了解一下yii框架的工作原理,同时学习一下优秀的编码规范和风格.在此记录一下阅读中的小心得. 每个框架都有一个入口文件,首先从入口文件开始,yii2的入口文件位于we ...
- Yii源码阅读笔记(三十五)
Container,用于动态地创建.注入依赖单元,映射依赖关系等功能,减少了许多代码量,降低代码耦合程度,提高项目的可维护性. namespace yii\di; use ReflectionClas ...
- Yii源码阅读笔记(十五)
Model类,集中整个应用的数据和业务逻辑——验证 /** * Returns the attribute labels. * 返回属性的标签 * * Attribute labels are mai ...
- Yii源码阅读笔记(二十五)
Module类中剩余部分代码,通过控制器ID实例化当前模块的控制器,当前模块的Action方法的前置和后置方法: /** * This method is invoked right before a ...
- Yii源码阅读笔记(八)
前面阅读了Yii2的两个基本类Object和Component,了解了Yii的三个重要概念属性.事件.行为,下面开始阅读Event类,Event类是所有事件类的基类: <?php /** * @ ...
- Yii源码阅读笔记(三)
接着上次的继续阅读BaseYii.php vendor/yiisoft/yii2/BaseYii.php—— public static function getRootAlias($alias)// ...
- Yii源码阅读笔记(二)
接下来阅读BaseYii.php vendor/yiisoft/yii2/BaseYii.php—— namespace yii; use yii\base\InvalidConfigExceptio ...
- Yii源码阅读笔记(三十四)
Instance类, 表示依赖注入容器或服务定位器中对某一个对象的引用 namespace yii\di; use Yii; use yii\base\InvalidConfigException; ...
- Yii源码阅读笔记(三十三)
ServiceLocator,服务定位类,用于yii2中的依赖注入,通过以ID为索引的方式缓存服务或则组件的实例来定位服务或者组件: namespace yii\di; use Yii; use Cl ...
随机推荐
- Java使用代理Proxy访问网络及其验证
在Java的网络编程中,有时候内网服务器需要访问外网的网络资源,这时候就需要使用代理. 设置代理(Proxy)可以有两种方式: 1.通过设置系统属性(System.setPropery(String ...
- 分布式缓存 memcache学习
1.使用分布式缓存是为了解决多台机器共享信息的问题,通过访问一个ip和端口来可以访问不同的IIS服务器 2.memcache基础原理 在Socket服务器端存储数据是以键值对的形式存储 内存处理的算法 ...
- Eclipse中项目面板字体的修改
修改eclipse安装目录中的如下文件,添加黄色标记部分,并设定自己需要的字体大小(这里是10px)即可: \eclipse\plugins\org.eclipse.ui.themes_1.1.1.v ...
- 实例讲解虚拟机3种网络模式(桥接、nat、Host-only)
转自:http://www.cnblogs.com/ggjucheng/archive/2012/08/19/2646007.html 前言 很多人安装虚拟机的时候,经常遇到不能上网的问题,而vmwa ...
- POJ 1523 SPF tarjan求割点
SPF Time Limit: 1000MS Memory Limit ...
- _EPROCESS结构简单了解!
_EPROCESS结构简单了解! lkd> dt _EPROCESSnt!_EPROCESS +0x000 Pcb : _KPROCESS +0x06c Pro ...
- 输入文本框,当点击enter时,做进一步处理!
//登录用户文本框敲回车键 private void txtCode_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.En ...
- 【转】HADOOP HDFS BALANCER介绍及经验总结
转自:http://www.aboutyun.com/thread-7354-1-1.html 集群平衡介绍 Hadoop的HDFS集群非常容易出现机器与机器之间磁盘利用率不平衡的情况,比如集群中添加 ...
- 关于sqoop与datax。 和sqoop to oracle插件OraOop
之前我还在想了解下datax,是否有可能替换sqoop,但了解后发现,datax和sqoop的业务场景是不同的.前者适合异构数据库的同步,后者适合hdfs与rdbms互相之间的同步.针对sq ...
- Eval有什么功能?
它的功能是把对应的字符串解析成JS代码并运行.应该尽量避免使用eval,因为不安全,非常耗性能.解析成JS代码要耗能,执行时也要耗能.