Closure::bindTo

(PHP 5 >= 5.4.0)

Closure::bindTo — Duplicates the closure with a new bound object and class scope 

说明

public Closure Closure::bindTo( object $newthis[, mixed $newscope  = 'static'] )

Create and return a new anonymousfunction with the same body and bound variables as this one, butpossibly with a different bound object and a new class scope. 

The “bound object” determines the value $this willhave in the function body and the “class scope” represents a classwhich determines which private and protected members the anonymousfunction will be able to access. Namely, the members that will bevisible are the same as if the anonymous function were a method ofthe class given as value of the newscopeparameter. 

Static closures cannot have any bound object (the value of the parameter newthis should be NULL), but this function cannevertheless be used to change their class scope. 

This function will ensure that for a non-static closure, having a boundinstance will imply being scoped and vice-versa. To this end,non-static closures that are given a scope but a NULL instance are madestatic and non-static non-scoped closures that are given a non-nullinstance are scoped to an unspecified class. 

Note: 

If you only want to duplicate the anonymous functions, you can use cloning instead. 

参数

newthis 

The object to which the given anonymous function should be bound, or NULL for the closure to be unbound. 

newscope 

The class scope to which associate the closure is to be associated, or'static' to keep the current one. If an object is given, the type of theobject will be used instead. This determines the visibility of protectedand private methods of the bound object. 

返回值

Returns the newly created Closure object 或者在失败时返回 FALSE 

范例

Example #1 Closure::bindTo() example

<?php

class A {

    function __construct($val) {

        $this->val = $val;

    }

    function getClosure() {

        //returns closure bound to this object and scope

        return function() { return $this->val; };

    }

}

$ob1 = new A(1);

$ob2 = new A(2);

$cl = $ob1->getClosure();

echo $cl(), "\n";

$cl = $cl->bindTo($ob2);

echo $cl(), "\n";

?>  

以上例程的输出类似于:

1

2

参见

◾Anonymous functions

◾ Closure::bind() - Duplicates a closure with a specific bound object and class scope

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

热门产品

触发修改文章时间【fastadmincms开发记录】|fastadmincms二次开发,触发,修改,文章,时间,fastadmin,ms,开发,记录
触发修改文章时间【fastadmincms开发记录】
在tp5中过滤输入的零宽度字符【fastadmincms开发记录】|fastadmincms二次开发,在tp5中,过滤,输入,的零,宽度,字符,fastadmin,ms,开发,记录
在tp5中过滤输入的零宽度字符【fastadmincms开发记录】
处理tag标签中的0宽空格【fastadmincms开发记录】|fastadmincms二次开发,处理,tag,标签,中的,0宽,空格,fastadmin,ms,开发,记录
处理tag标签中的0宽空格【fastadmincms开发记录】
添加专题时tags标签id出错【fastadmincms开发记录】|fastadmincms二次开发,添加,专题,时tags,标签,id,出错,fastadmin,ms,开发,记录
添加专题时tags标签id出错【fastadmincms开发记录】
20230518----模板 广告【fastadmincms开发记录】|fastadmincms二次开发,20230518,模板,广告,fastadmin,ms,开发,记录
20230518----模板 广告【fastadmincms开发记录】
cms添加视频模型【fastadmincms开发记录】|fastadmincms二次开发,ms,添加,视频,模型,fastadmin,开发,记录
cms添加视频模型【fastadmincms开发记录】
新增单篇收费复制功能【fastadmincms开发记录】|fastadmincms二次开发,新增,单篇,收费,复制,功能,fastadmin,ms,开发,记录
新增单篇收费复制功能【fastadmincms开发记录】
添加开会员折扣功能【fastadmincms开发记录】|fastadmincms二次开发,添加,开会,折扣,功能,fastadmin,ms,开发,记录
添加开会员折扣功能【fastadmincms开发记录】

历史上的今天:05月02日

热门专题

卓越综合高中|卓越综合高中
卓越综合高中
云南巨榕教育投资集团有限公司|云南巨榕教育投资集团有限公司,巨榕教育集团,巨榕教育
云南巨榕教育投资集团有限公司
金诺幼儿园(春城路金诺幼儿园)|昆明官渡区幼儿园,幼儿园报名,官渡区幼儿园,春城路幼儿园,幼儿园招生,学前班,昆明幼儿园,金诺幼儿园,环城南路幼儿园,石井路幼儿园
金诺幼儿园(春城路金诺幼儿园)
大理科技管理学校|大理科技管理学校,大理科技,大理科技中等职业技术学校,大理科技管理中等职业技术学校,大理科技学校
大理科技管理学校
中源管业|中源管业,中源管业公司,中源管业有限公司,中源管业电话,中源管业地址,中源管业电力管,中源管业mpp电力管,中源管业cpvc电力管,中源管业pe穿线管
中源管业
天麻的功效与作用吃法|天麻的功效与作用,天麻的功效与作用吃法,天麻炖什么治头痛最好,天麻的功效与作用禁忌,天麻多少钱一斤,天麻的功效与作用吃法及禁忌,天麻怎么吃效果最好,天麻粉的功效与作用,天麻怎么吃
天麻的功效与作用吃法
小程序开发|微信小程序,小程序开发,小程序,小程序制作,微信小程序开发,小程序公司,小程序开发公司,分销,三级分销系统,分销系统
小程序开发
大理科技管理学校|大理科技管理中等职业技术学校,大理市科技管理中等职业技术学校
大理科技管理学校

微信小程序

微信扫一扫体验

立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部