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
相关文章
|
6月前
|
JSON 前端开发 JavaScript
JavaWeb基础8——Filter,Listener,Ajax,Axios,JSON
Filter过滤器、Listener监听器、AJAX、 同步、异步优点和使用场景、Axios异步框架、JSON、js和JSON转换、案例,Axios + JSON 品牌列表查询和添加
JavaWeb基础8——Filter,Listener,Ajax,Axios,JSON
|
6月前
|
存储 JavaScript 前端开发
JS篇(Array、Object)
JS篇(Array、Object)
41 1
|
10月前
|
JavaScript
js 字符串String转对象Object
该代码示例展示了如何将一个以逗号分隔的字符串(`'1.2,2,3,4,5'`)转换为对象数组。通过使用`split(',')`分割字符串并`map(parseFloat)`处理每个元素,将字符串转换成浮点数数组,最终得到一个对象数组,其类型为`object`。
407 2
|
JavaScript 前端开发
js中 object转为string
在Javascript 中,从excel中获取的数字为Object类型,可以用JSON.stringify(obj)方法将obj转为string类型。
|
安全 前端开发 PHP
yii2.0里面的HtmlPurifier::process是干什么的?
yii2.0里面的HtmlPurifier::process是干什么的?
277 0
|
安全 API 开发工具
yii2.0的yii\authclient\Collection是干什么的?底层原理是什么?
yii2.0的yii\authclient\Collection是干什么的?底层原理是什么?
151 0
Object static methods
Object static methods
73 0
|
JavaScript
JavaScript---网络编程(3)-Object、String、Array对象和prototype属性-2
JavaScript---网络编程(3)-Object、String、Array对象和prototype属性-1
194 0
JavaScript---网络编程(3)-Object、String、Array对象和prototype属性-2
|
JavaScript 前端开发 索引
JavaScript---网络编程(3)-Object、String、Array对象和prototype属性-1
JavaScript---网络编程(3)-Object、String、Array对象和prototype属性
190 0
JavaScript---网络编程(3)-Object、String、Array对象和prototype属性-1
|
JavaScript 前端开发 索引
第190天:js---String常用属性和方法(最全)
String常用属性和方法 一、string对象构造函数 1 /*string对象构造函数*/ 2 console.log('字符串即对象');//字符串即对象 3 //传统方式 - 背后会自动将其转换成对象 4 // 所以我们才可以访问string对象中方法 5 var zhangsan ='张三' 6 zhangsan.
1317 0
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等