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 ...
随机推荐
- 20个命令行工具监控Linux系统性能
作为Linux/Unix 系统管理员需要掌握一些常用的工具用于检测系统性能.在这里,dodo为大家推荐非常20个有用的并且最常用的命令行系统监视工具: 1. top -Linux系统进程监控 top ...
- 如何以nobody用户执行命令?
最近在logstash中使用nobody用户启动logstash,一想,nobody用户的shell不是/sbin/nologin吗? 不能登录执行命令呀? 于是看了一下它的启动脚本,是使用其他方式进 ...
- 自然语言6_treebank句子解析
#英文句子结构分析 import nltkfrom nltk.corpus import treebankt = treebank.parsed_sents('wsj_0001.mrg')[1]t.d ...
- HDFS的Java操作
实验环境: Windows 10 Eclipse Mars.2 Release (4.5.2) CentOS 7 Hadoop-2.7.3 先决条件: 1) Windows上各环境变量已配置OK. ...
- Numerical Optimization: Understanding L-BFGS
http://aria42.com/blog/2014/12/understanding-lbfgs/ Numerical optimization is at the core of much of ...
- css div垂直居中
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- iOS监听键盘事件
#pragma mark - view life cycle - (void)viewDidLoad { [super viewDidLoad]; [[NSNotificationCenter def ...
- 服务器后台TCP连接存活问题
0. 背景 公司的服务器后台部署在某一个地方,接入的是用户的APP,而该地方的网络信号较差,导致了服务器后台在运行一段时间后用户无法接入,那边的同事反馈使用netstat查看系统,存在较多的TCP连接 ...
- Fiddler进行模拟Post提交json数据,总为null解决方式
Request Headers: User-Agent: FiddlerHost: localhost:3248Content-Type: application/json; charset=utf- ...
- lombok+slf4j+logback SLF4J和Logback日志框架详解
maven 包依赖 <dependency> <groupId>org.projectlombok</groupId> <artifactId>lomb ...