PHP Closure类Bind与BindTo方法
Closure类为闭包类,PHP中闭包都是Closure的实例:
1 $func = function(){};
2 var_dump($func instanceof Closure);
输出 bool(true)
Closure有两个函数将闭包函数绑定到对象上去,
静态方法Bind
public static Closure Closure::bind ( Closure $closure , object $newthis [, mixed $newscope = 'static' ] )
动态方法BindTo
public Closure Closure::bindTo ( object $newthis [, mixed $newscope = 'static' ] )
静态闭包不能有绑定的对象($newthis 参数的值应该设为 NULL )
此时Closure不可以使用$this。
class Father
{
public $pu = "public variable"; public static $spu = 'public static';
} class Son extends Father
{ } class Other
{ } $son = new Son();
$func = function(){
echo self::$spu;
}; ($func -> bindTo(null, 'Son'))();
静态闭包中不可以调用$this,否则会报错。就像类的静态方法不可以调用$this一样
$son = new Son();
$func = function(){
echo $this -> $pu;
}; ($func -> bindTo(null, 'Son'))();
报错:
Fatal error: Uncaught Error: Using $this when not in object context in D:\laravel\test.php:21
Stack trace:
类作用域:
当闭包绑定到对象上时,或者绑定到null成为静态对象,可以通过返回的闭包对象来调用对象的方法,同时可以设定第三个参数$newscope来设定对象中
属性或方法对于闭包的访问可见性。闭包的访问可见性和$newscope类的成员函数是相同的。
class Father
{
protected $pu = "public variable"; protected static $spu = 'public static';
} class Son extends Father
{ }
//Son中的方法可以正常访问Father类中的protected属性 class Other
{ }
//Other中的方法无法访问Father类中的protected属性
测试:
$son = new Son();
$func = function(){
echo $this -> pu;
};
(Closure::bind($func, $son, 'Son'))();
输出 public variable
(Closure::bind($func, $son, 'Other'))();
报错 Fatal error: Uncaught Error: Cannot access protected property Son::$pu
匿名函数都是Closure的实例所以可以调用 bindTo 方法。
bindTo方法
($func -> bindTo($son, 'Son'))();
($func -> bindTo($son, 'Other'))();
$newscope默认为‘Static'表示不改变,还是之前的作用域。
class Grand
{
protected $Grandvar = 'this is grand';
} class Father extends Grand
{
protected $Fathervar = "this is Father";
} class Mother extends Grand
{
protected $Mothervar = 'this is Mother';
} class Son extends Father
{
protected $Sonvar = 'this is son';
} $son = new Son();
$mon = new Mother(); $func = function(){
echo $this -> Grandvar;
}; 绑定访问作用域为'Son'的作用域,之后使用之前的默认作用域,
所以可以访问Grandvar
$newFunc = Closure::bind($func, $son, 'Son');
$newFunc = Closure::bind($newFunc, $mon);
$newFunc(); 未绑定访问作用域,默认没有权限
$newFunc = Closure::bind($func, $mon);
$newFunc();
http://php.net/manual/zh/class.closure.php
PHP Closure类Bind与BindTo方法的更多相关文章
- php中怎么理解Closure的bind和bindTo
bind是bindTo的静态版本,因此只说bind吧.(还不是太了解为什么要弄出两个版本) 官方文档: 复制一个闭包,绑定指定的$this对象和类作用域. 其实后半句表述很不清楚. 我的理解: 把一个 ...
- php Closure类 闭包 匿名函数
php匿名函数 匿名函数就是没有名称的函数.匿名函数可以赋值给变量,还能像其他任何PHP对象那样传递.不过匿名函数仍是函数,因此可以调用,还可以传入参数.匿名函数特别适合作为函数或方法的回调. 如: ...
- ES6语法~解构赋值、箭头函数、class类继承及属性方法、map、set、symbol、rest、new.target、 Object.entries...
2015年6月17日 ECMAScript 6发布正式版本 前面介绍基本语法, 后面为class用法及属性方法.set.symbol.rest等语法. 一.基本语法: 1. 定义变 ...
- Atitti 载入类的几种方法 Class.forName ClassLoader.loadClass 直接new
Atitti 载入类的几种方法 Class.forName ClassLoader.loadClass 直接new 1.1. 载入类的几种方法 Class.forName ClassLo ...
- java进阶之反射:反射基础之如何获取一个类以及如何获取这个类的所有属性和方法(2)
当我们知道一个类的对象,或者知道一个类的路径,或者指导这个类的名称的时候我们可以获取到这个类的类对象 当我们仅仅知道一个类的类对象的时候我们依然无法操作这个类,因为我们不知道这个类的属性,类的方法.那 ...
- JQuery操作类数组的工具方法
JQuery学习之操作类数组的工具方法 在很多时候,JQuery的$()函数都返回一个类似数据的JQuery对象,例如$('div')将返回div里面的所有div元素包装的JQuery对象.在这中情况 ...
- Python - 类与对象的方法
类与对象的方法
- Java中是否可以调用一个类中的main方法?
前几天面试的时候,被问到在Java中是否可以调用一个类中的main方法?回来测试了下,答案是可以!代码如下: main1中调用main2的主方法 package org.fiu.test; impor ...
- [Q&A] 类Range的PasteSpecial方法无效
环境说明: VS2013(C#) + Office2013 Bug说明: range1.Copy(Type.Missing); range2.PasteSpecial(Excel.XlPasteTyp ...
随机推荐
- CSS3系列一(概述、选择器、使用选择器插入内容)
CSS3模块化结构 CSS历史发展 CSS(Cascading Style Sheet),层叠样式表,是用于控制网页样式并允许将样式信息与网页内容分离的一种标记性语言. CSS3属性选择器 E[att ...
- String.format()用法
package junit.test; import java.util.Date; import java.util.Locale; import org.junit.Test; pub ...
- BZOJ1004: [HNOI2008]Cards
三维01背包算出在每一个置换下不变的染色方案数,Burnside引理计算答案. PS:数据太水所以只算恒等置换也是可以过的. #include<bits/stdc++.h> using n ...
- SQL查询效率:100w数据查询只需要1秒钟
G os: windows 数据库: ms sql server 目的: 查询性能测试,比较两种查询的性能 SQL查询效率 step by step -- setp . -- 建表 create ta ...
- 如何修改Linux主机名
Linux 下什么都比较麻烦,就连修改主机名也不例外.我们就下文说一下具体方法. Linux 安装好后,其默认的主机名是 localhost.修改 Linux 主机名需要3步. 使用 hostname ...
- Centos: 修改 yum安装的mysql路径
1.使用命令service mysqld stop 停止mysql查看mysql数据库的默认路径:/var/lib/mysql使用cp -afir /var/lib/mysql/* /usr/l ...
- GLEW OpenGL Access violation when using glGenVertexArrays
http://stackoverflow.com/questions/20766864/glew-opengl-access-violation-when-using-glgenvertexarray ...
- 分享jquery实现百叶窗特效的图片轮播
首先非常感谢网友嘉翼的无私分享,这是他刚在网站扣下来的特效,第一时间与大家分享,jquery实现百叶窗特效的图片轮播 使用方法: 1.引用css文件,css文件里面已经做了注释,基本只需要修改宽高就好 ...
- yii2权限控制rbac之rule详细讲解(转)
在我们之前yii2搭建后台以及rbac详细教程中,不知道你曾经疑惑过没有一个问题,rule表是做什么的,为什么在整个过程中我们都没有涉及到这张表? 相信我不说,部分人也都会去尝试,或百度或google ...
- motto1
不要失去了才懂得珍惜.很多东西是不能再来的,又有很多东西再来是要付出代价的,所以,好好珍惜你现在拥有的一切,努力去争取你现在没有的.