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 ...
随机推荐
- Mac下同时安装多个版本的JDK
JDK8 GA之后,小伙伴们喜大普奔,纷纷跃跃欲试,想体验一下Java8的Lambda等新特性,可是目前Java企业级应用的主打版本还是JDK6, JDK7.因此,我需要在我的电脑上同时有JDK8,J ...
- Timer类和TimerTask类
Timer类是一种线程设施,可以用来实现在某一个时间或某一段时间后安排某一个任务执行一次或定期重复执行. 该功能要与TimerTask类配合使用.TimerTask类用来实现由Timer安排的一次或重 ...
- Java观察者设计模式
在java.util包中提供了Observable类和Observer接口,使用它们即可完成观察者模式. 多个观察者都在关注着价格的变化,只要价格一有变化,则所有的观察者会立即有所行动. //==== ...
- Java I/O流体系
- Linux基础整理-软件的安装与卸载
redhat/centos/fedora/suse系列: 摘自网址:http://www.runoob.com/linux/linux-yum.html yum( Yellow dog Updater ...
- Docker Compose to CoreOS
taken from https://docs.docker.com/compose/install/ the only thing is that /usr is read only, but /o ...
- Autofac.Integration.Web分析
using System; using System.Web; using Autofac.Core.Lifetime; namespace Autofac.Integration.Web { /// ...
- java enum类
1.可以在enum中添加变量和方法 先来看一段代码示例: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 ...
- Semantic ui 学习笔记 持续更新
这个semantic 更新版本好快~ 首先是代码的标识<code></code> 具体样式就是红框这样的 圈起来代码感觉不错 不过要在semantic.css里在加上如下样式~ ...
- DataTable转实体
public class ModelConvertHelper<T> where T : new() { public static IList<T> ConvertToMod ...