Yii2 理解Object
来源:互联网 发布:人工智能语音计算器 编辑:程序博客网 时间:2024/05/01 22:25
- 版本
- 继承与实现
- 构造函数 __construct
- __get __set
- method_exists
- __isset __unset
- 其余判断函数
1 版本
// yii\BaseYii\getVersionpublic 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
hasPropertycanGetPropertycanSetPropertyhasMethod
0 0
- Yii2 理解Object
- Yii2 理解Component
- Yii2 理解Controller
- Yii2 理解filters
- Yii2 理解Validator
- Yii2 理解di
- 深入理解 Yii2.0
- yii2框架-理解yii2的架构(一)
- Yii2框架-理解Yii2的架构
- Yii2中Component和Object的使用方法
- Yii2 使用十九 Object对象机制
- Yii2框架bootstrap样式理解
- Yii2框架bootstrap样式理解
- 理解synchronized(Object)锁
- 对象(Object)理解
- 理解object slicing
- 理解Javascript_09_Function与Object
- 理解Object转String
- 算法导论 第十一章 散列表
- 26. Remove Duplicates from Sorted Array
- 指针
- VR系列——Oculus Audio sdk文档:八、Oculus Spatializer AAX集成指南(1~2)——概述及使用插件
- java中的Servlet
- Yii2 理解Object
- TensorFlow官方教程学习笔记(二)——MNIST入门
- Linux 入门笔记3 —— 安装第三方库
- orge terrain
- java中的Servlet(二)
- python第九天
- QuickBI中如何实现向下钻取的动态地图仪表板
- java字符流
- Yii2 创建项目