Yii2 理解Object

简介: 版本继承与实现构造函数 __construct__get __setmethod_exists__isset __unset其余判断函数1 版本// yii\BaseYii\getVersionpublic static function getVersion(){ return '2.0.10';}2 继

1 版本

// yii\BaseYii\getVersion
public static function getVersion()
{
    return '2.0.10';
}

2 继承与实现

Object实现了Configurable接口。
Configureable要求在构造函数的参数末尾加上$config

public function __constructor($param1, $param2, ..., $config = [])

3 构造函数 __construct

// 按照Configureable要求, 需要在参数末尾添加 $config = []
public function __construct($config = [])
{
    if (!empty($config)) {
        Yii::configure($this, $config);
    }
    // 调用了初始化函数
    $this->init();
}

4 __get, __set

// 重写了php5中预定义的__get
// php5中的代码:
public function __get($name)
{ 
    return $this->$name; 
} 
// yii中的代码
public function __get($name)
{
    // 由原来的直接调用属性改为通过调用get函数来间接调用
    $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);
    }
}

同理于__set
在代码中可以看见,如果找不到$getter, 却找到了setter, 就会报出异常,这个属性不可读。

5 method_exists

用于检测类中指定的函数是否存在

6 __isset, __unset

__isset 用于判断某个属性是否定义了
__unset 用于将某个属性设置为null,但是对只读的属性,会报异常

最好不要直接使用__isset和__unset,而是使用isset和unset

public function __isset($name)
{
    $getter = 'get' . $name;
    if (method_exists($this, $getter)) 
    {
        return $this->$getter() !== null;
    } 
    else 
    {
        return false;
    }
}
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);
    }
}

7 其余判断函数

以下函数基本上借助于method_exists和property_exists

hasProperty
canGetProperty
canSetProperty
hasMethod
相关文章
|
9月前
|
PHP
thinkphp报错Call to undefined method app\index\controller\Index::fetch()
thinkphp报错Call to undefined method app\index\controller\Index::fetch()
91 0
|
数据库
yii2.0的Class yii\widgets\LinkPager是干什么的?底层原理是什么?
yii2.0的Class yii\widgets\LinkPager是干什么的?底层原理是什么?
|
安全 API 开发工具
yii2.0的yii\authclient\Collection是干什么的?底层原理是什么?
yii2.0的yii\authclient\Collection是干什么的?底层原理是什么?
106 0
|
应用服务中间件 Apache nginx
YII2.0里面的'showScriptName' => false是干什么的?
YII2.0里面的'showScriptName' => false是干什么的?
115 0
|
前端开发
九十八、freemarker框架报错 s.e.ErrorMvcAutoConfiguration$StaticView : Cannot render error page for request
九十八、freemarker框架报错 s.e.ErrorMvcAutoConfiguration$StaticView : Cannot render error page for request
1063 0
九十八、freemarker框架报错 s.e.ErrorMvcAutoConfiguration$StaticView : Cannot render error page for request
|
网络架构
CodeIgniter报错: You must use the "set" method to update an entry
I'm using codeigniter/datamapper to develop an inviocing application and I'm getting an error that i don't understand.
2089 0
|
JSON 数据格式
create a new JSON model with url will trigger SAP UI5 AJAX
Created by Wang, Jerry, last modified on May 01, 2016
90 0
create a new JSON model with url will trigger SAP UI5 AJAX
|
JavaScript 前端开发
JavaScript中Object.keys、Object.getOwnPropertyNames区别
定义 Object.keys 定义:返回一个对象可枚举属性的字符串数组; Object.getOwnPropertyNames 定义:返回一个对象可枚举、不可枚举属性的名称;   属性的可枚举性、不可枚举性 定义:可枚举属性是指那些内部 “可枚举” 标志设置为 true 的属性,对于通过直接的赋值和属性初始化的属性,该标识值默认为即为 true,对于通过 Object.defineProperty 等定义的属性,该标识值默认为 false。
1085 0