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方法的更多相关文章

  1. php中怎么理解Closure的bind和bindTo

    bind是bindTo的静态版本,因此只说bind吧.(还不是太了解为什么要弄出两个版本) 官方文档: 复制一个闭包,绑定指定的$this对象和类作用域. 其实后半句表述很不清楚. 我的理解: 把一个 ...

  2. php Closure类 闭包 匿名函数

    php匿名函数 匿名函数就是没有名称的函数.匿名函数可以赋值给变量,还能像其他任何PHP对象那样传递.不过匿名函数仍是函数,因此可以调用,还可以传入参数.匿名函数特别适合作为函数或方法的回调. 如: ...

  3. ES6语法~解构赋值、箭头函数、class类继承及属性方法、map、set、symbol、rest、new.target、 Object.entries...

    2015年6月17日 ECMAScript 6发布正式版本 前面介绍基本语法,  后面为class用法及属性方法.set.symbol.rest等语法. 一.基本语法:  1.         定义变 ...

  4. Atitti 载入类的几种方法    Class.forName ClassLoader.loadClass  直接new

    Atitti 载入类的几种方法    Class.forName ClassLoader.loadClass  直接new 1.1. 载入类的几种方法    Class.forName ClassLo ...

  5. java进阶之反射:反射基础之如何获取一个类以及如何获取这个类的所有属性和方法(2)

    当我们知道一个类的对象,或者知道一个类的路径,或者指导这个类的名称的时候我们可以获取到这个类的类对象 当我们仅仅知道一个类的类对象的时候我们依然无法操作这个类,因为我们不知道这个类的属性,类的方法.那 ...

  6. JQuery操作类数组的工具方法

    JQuery学习之操作类数组的工具方法 在很多时候,JQuery的$()函数都返回一个类似数据的JQuery对象,例如$('div')将返回div里面的所有div元素包装的JQuery对象.在这中情况 ...

  7. Python - 类与对象的方法

    类与对象的方法

  8. Java中是否可以调用一个类中的main方法?

    前几天面试的时候,被问到在Java中是否可以调用一个类中的main方法?回来测试了下,答案是可以!代码如下: main1中调用main2的主方法 package org.fiu.test; impor ...

  9. [Q&A] 类Range的PasteSpecial方法无效

    环境说明: VS2013(C#) + Office2013 Bug说明: range1.Copy(Type.Missing); range2.PasteSpecial(Excel.XlPasteTyp ...

随机推荐

  1. C#调用c++Dll 结构体数组指针的问题

    参考文章http://blog.csdn.net/jadeflute/article/details/5684687 但是这里面第一个方案我没有测试成功,第二个方案我感觉有点复杂. 然后自己写啦一个: ...

  2. 和redis谈一场恋爱(第一天邂逅)

    前几天玩了下Memcache,发现挺好用.知道redis是Memcache的妹妹.我本着大公无私和博大的胸怀,看着redis孤零零的躺在角落里,委实觉得可怜.心里总有个声音在说,你既然已经爱上了Mem ...

  3. 用面对对象方式定tab标签

    一些公共的底层的JS方法 var GLOBAL = {}; GLOBAL.namespace = function (str) { var arr = str.split('.'), o = GLOB ...

  4. centos 搭建git服务器

    centos 6搭建git服务器 安装 rpm -ivh http://mirrors.aliyun.com/epel/epel-release-latest-6.noarch.rpm yum ins ...

  5. Yocto开发笔记之《应用程序架构》(QQ交流群:519230208)

    QQ群:519230208,为避免广告骚扰,申请时请注明 “开发者” 字样 ======================================================== Eclip ...

  6. Maven概览

    Maven的核心思想,约定由于配置 1 Maven坐标 1.1 本项目的坐标 groupId: 必须.项目组名称,定义当前Maven项目所隶属的实际项目,通常与域名反向一一对应,与Java包名表示方式 ...

  7. Linux启动盘制作

    1.下载Universal-USB-Installer软件,在电脑上插入空U盘,打开软件,依下列所示步骤进行 2.点击同意 3.点击版本选择框,会出现它支持的系统镜像文件的版本,包括win7.win8 ...

  8. Orchard源码分析(4.2):Orchard.Logging.LoggingModule类

    与CollectionOrderModule一样,LoggingModule也是一个Autofac模块.它以属性注入的方式给需要日志服务的对象设置Logger.    如果一个类有Orchard.Lo ...

  9. C++ security issue analyze

    https://sploitfun.wordpress.com/about-2/ “Happiness is only real when shared” – Into the wild http:/ ...

  10. Struts2源码浅析-ConfigurationProvider

    ConfigurationProvider接口 主要完成struts配置文件 加载 注册过程 ConfigurationProvider接口定义 public interface Configurat ...