Yii2 提供可以用属性的方式去获取类的一个方法
刚开始用 Yii 的小朋友可能对下面的写法非常疑惑:
public function actionIndex()
{
$user = User::find()->where(['name'=>'zhangsan'])->one();
$user->orders; // 关联查询订单表
}
去 User 的 Model 去找 orders 属性也没找到,这个是什么实现的?为什么可以这样写?
其实这个只是 PHP 魔术方法的__get的一种实现。
为了更高的访问了我们的数据,Yii2 提供了可以用属性的方式去获取类的一个方法,Demo如下:
// User Model public function getOrders()
{
return $this->hasMany(Order::className(), ['user_id' => 'id']);
} // User Controller public function actionView($id)
{
$user = User::find()->where(['name'=>'zhangsan'])->one();
$user->orders; // 此处访问的就是 getOrders() 方法
}
追溯到源代码(yii2\base\Component.php):
/**
* Returns the value of a component property.
* This method will check in the following order and act accordingly:
*
* - a property defined by a getter: return the getter result
* - a property of a behavior: return the behavior property value
*
* Do not call this method directly as it is a PHP magic method that
* will be implicitly called when executing `$value = $component->property;`.
* @param string $name the property name
* @return mixed the property value or the value of a behavior's property
* @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;
if (method_exists($this, $getter)) {
// read property, e.g. getName()
return $this->$getter();
} else {
// behavior property
$this->ensureBehaviors();
foreach ($this->_behaviors as $behavior) {
if ($behavior->canGetProperty($name)) {
return $behavior->$name;
}
}
}
if (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);
}
}
Yii2 提供可以用属性的方式去获取类的一个方法的更多相关文章
- Android 通过接口的方式去调用服务里面的方法
public class MainActivity extends AppCompatActivity { private MyConn conn; private Iservice myBinder ...
- springboot读取系统级环境变量,和读写系统属性以及unittest来获取环境变量的方法
环境变量的读取以及系统属性的设置 环境变量只能读取,不能修改,系统属性可以修改 系统变量的读取方式: System.getEnv() 系统属性有多重读取和修改方式: 其修改方式为: 读取系统属性: @ ...
- 访问类的私有属性(RTTI和模仿类2种方法)
如何访问类的私有属性? 下面以 TPathData 为例,它有一个私有属性 PathData,储存了每一个曲线点,但一般无法修改它,需要利用下面方法,才能访问修改(若有更好的方法,歡迎分享): 一.利 ...
- python 面向对象类成员(字段 方法 属性)
一.字段 字段包括:普通字段和静态字段,他们在定义和使用中有所区别,而最本质的区别是内存中保存的位置不同, 普通字段属于对象 静态字段属于类 class Province: # 静态字段 countr ...
- 浅谈Java反射机制 之 获取类的 方法 和 属性(包括构造函数)
上一篇 获取 类 的字节码文件 我们讲到了获取类的字节码文件的三种方法 第三种方法通过getClass("全路径名")获取字节码文件最符合要求 1.获取构造方法 先贴上我们要获取的 ...
- windev的内部窗口传参方式及其与类的相似性
最近的应用,需要向一个内部窗口(internal window)传参,因为官方文档的说明较为宽泛,虽然结果只有两小段代码,但也费了很大的劲.把所有关于procedure的文档看一遍,又是重新学习了一遍 ...
- 反射-优化及程序集等(用委托的方式调用需要反射调用的方法(或者属性、字段),而不去使用Invoke方法)
反射-优化及程序集等(用委托的方式调用需要反射调用的方法(或者属性.字段),而不去使用Invoke方法) 创建Delegate (1).Delegate.CreateDelegate(Type, ...
- Yii2基本概念之——属性(property)
学习任何一门学问,往往都是从起基本的概念学起.万丈高楼平地起,这些基本概念就是高楼的基石,必须做详尽的分析.我们知道,Yii2是一款脉络清晰的框架,理顺了基础的概念和基本功能,学习更高级和复杂的功能就 ...
- Django中提供的6种缓存方式
由于Django是动态网站,所有每次请求均会去数据进行相应的操作,当程序访问量大时,耗时必然会更加明显,最简单解决方式是使用: 缓存,缓存将一个某个views的返回值保存至内存或者memcache中, ...
随机推荐
- NoSQL之Redis学习小结
大数据时代要求: 三V:Volume海量.Velocity实时.Variety多样: 三高:高并发.高可扩.高性能 高并发操作不建议使用关联查询,而使用冗余数据,分布式系统支持不了太多的并发. 横向 ...
- Android XMPP 即时通讯
0 http://blog.csdn.net/lnb333666/article/details/7471292 0.1 http://www.cnblogs.com/charley_yang/arc ...
- asp.net core mvc视频A:笔记1.基本概念介绍
此笔记来自视频教程 MVC本身与三层架构没有联系 使用VS2017新建一个默认的asp.net core mvc网站,认识结构及文件用途.
- 在caffe中用训练好的 caffemodel 来分类新的图片所遇到的问题
结合之前的博客: http://www.cnblogs.com/Allen-rg/p/5834551.html#3949333 用caffemodel去测试单通道的图像(mnist数据集)时,出现了问 ...
- $.ajax 温故而知新坑
$.ajax的配置项中使用 contentType: "application/json" 时,Data选项允许为String类型,如JSON.stringify({abc:123 ...
- memcache原理和实际应用
Memcache是什么 Memcache是danga.com的一个项目,最早是为 LiveJournal 服务的.眼下全世界不少人使用这个缓存项目来构建自己大负载的站点,来分担数据库的压力. 它能够应 ...
- lua学习笔记(三)
类型与值 lua是一种动态类型的语言,在语言中没有类型定义的语法,每个值都携带了它自身的类型信息 lua中有8种基础类型 nil 只与自身相等assert(nil==nil ...
- getOutputStream() has already been called for this response的解决方法
1.问题描述:spring mvc中下载文件结束后,跳转到list页面,问题报上面的异常. 2.原因:写文件的时候response调用一次,在跳转的时候,spring调用ActionForward类中 ...
- java.io.IOException: Illegal partition for 67 (-1)
今天写MapReduce的分区进行排序的功能,自己写了一个Partitioner,然后用的时候就错了 public static class MyPartition extends Partition ...
- __must_check必须处理函数返回值
include/linux/compiler-gcc4.h #define __must_check __attribute__((warn_unused_result)) _ ...