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. .bash_profile for mac‘ envionment variables

    A typical install of OS X won't create a .bash_profile for you. When you want to run functions from ...

  2. 深入理解JavaScript中创建对象模式的演变(原型)

    深入理解JavaScript中创建对象模式的演变(原型) 创建对象的模式多种多样,但是各种模式又有怎样的利弊呢?有没有一种最为完美的模式呢?下面我将就以下几个方面来分析创建对象的几种模式: Objec ...

  3. 电脑技巧:Win8/Win10无法打开这个应用|无法使用内置管理员账户的完美解决方法

    现在装win10系统的同伴越来越多了,相比于win7,win10在某些设置方面也有些变化,比如我们在使用win8或者win10时,会碰到如图所示的对话框: Windows10/Windows8无法使用 ...

  4. Timer类和TimerTask类

    Timer类是一种线程设施,可以用来实现在某一个时间或某一段时间后安排某一个任务执行一次或定期重复执行. 该功能要与TimerTask类配合使用.TimerTask类用来实现由Timer安排的一次或重 ...

  5. 入门:HTML:hello world!

    <html> <head> </head> <body> <h1>hello world!</h1> </body> ...

  6. Java Native Interface Specification

    http://docs.oracle.com/javase/6/docs/technotes/guides/jni/spec/jniTOC.html http://www.ibm.com/develo ...

  7. AgularJS中Unknown provider: $routeProvider解决方案

    最近在学习AgularJS, 做到http://angularjs.cn/A00a这一步时发现没有办法执行路由功能, 后来翻看控制台日志,发现提示Unknown provider: $routePro ...

  8. LeetCode —— Invert Binary Tree

    struct TreeNode* invertTree(struct TreeNode* root) { if ( NULL == root ) { return NULL; } if ( NULL ...

  9. php Hash Table(三) Hash Table初始化

    HashTable初始化,在使用HashTable之前要先执行初始化,下边就看看初始化时都做了什么, Zend/zend_hash.c static const Bucket *uninitializ ...

  10. dns泛解析漫谈

    比如说:http://www.aaa.com/ 指向10.10.1.1,ftp.aaa.com/ 指向10.10.2.2,如果这时候客户访问的是aaa.com或者error.aaa.com (这里er ...