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 ...
随机推荐
- hdu 1045:Fire Net(DFS经典题)
Fire Net Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- html select 下拉箭头隐藏
html select 下拉箭头隐藏 <!DOCTYPE html> <html> <head lang="en"> <meta char ...
- Maven跳过test打包
1. cd到当前目录 2. mvn install -Dmaven.test.skip=true 参考:http://blog.csdn.net/symgdwyh/article/details/ ...
- linux socket 编程(C语言)
转自:http://blog.csdn.net/piaojun_pj/article/details/5920888 最近看了一些网络编程的书籍,一直以来总感觉网络编程神秘莫测,其实网络编程入门还是很 ...
- C调Lua
转自:http://foredoomed.org/blog/2013/12/07/integrate-c-with-lua/ 我们在用C写程序的时候,很多情况下需要用到List,Map等集合,但是C是 ...
- Android利用调试器调试程序
首先,通过在AndroidManifest.xml文件中添加android:debuggable=”true”选项, <application android:icon=”@drawable/i ...
- IEEE754测试-软件
为满足硬件开发的同事验证从传感器采集到的数据是否正确,也为了方便我自己, 随手做了这个小东西,主要涉及浮点数的存储问题!
- 【spring 配置文件】spring配置文件的解析
一.总体结构 二.详解 1.spring <alias >标签 在对bean进行定义时,除了使用id属性来指定名称之外,为了提供多个名称,可以使用alias标签来指定.而所有的这些名称都指 ...
- [saiku] 在 Tomcat 下部署 saiku
上一篇介绍了如何配置免登录[免登录进入管理台] 本文主要是一步一步解释如何在Tomcat下部署saiku,为了让你更好更快的使用. 开始之前,假设你已经比较熟悉数据集市数据库,以及如何建立Mondri ...
- 简单几何(线段相交)+模拟 POJ 3449 Geometric Shapes
题目传送门 题意:给了若干个图形,问每个图形与哪些图形相交 分析:题目说白了就是处理出每个图形的线段,然后判断是否相交.但是读入输出巨恶心,就是个模拟题加上线段相交的判断,我第一次WA不知道输出要按字 ...