Closure::bindTo

(PHP 5 >= 5.4.0, PHP 7)

Closure::bindTo 锟斤拷锟狡碉拷前锟秸帮拷锟斤拷锟襟,帮拷指锟斤拷锟斤拷$this锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟�

说锟斤拷

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

锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷一锟斤拷 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷 锟斤拷锟诫当前锟斤拷锟斤拷暮锟斤拷锟斤拷锟斤拷锟酵拷锟斤拷锟斤拷锟酵拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷园蠖ú锟酵拷亩锟斤拷锟揭诧拷锟斤拷园锟斤拷碌锟斤拷锟斤拷锟斤拷锟斤拷锟�

"锟襟定的讹拷锟斤拷"锟斤拷锟斤拷锟剿猴拷锟斤拷锟斤拷锟叫碉拷 $this 锟斤拷取值锟斤拷"锟斤拷锟斤拷锟斤拷锟斤拷"锟斤拷锟斤拷一锟斤拷锟斤拷锟酵★拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷芄锟斤拷锟斤拷锟斤拷锟叫� 私锟斤拷 锟斤拷 锟斤拷锟斤拷 锟侥凤拷锟斤拷锟斤拷 也锟斤拷锟斤拷说锟斤拷锟斤拷时 $this 锟斤拷锟皆碉拷锟矫的凤拷锟斤拷锟斤拷锟斤拷 newscope 锟斤拷某锟皆憋拷锟斤拷锟斤拷锟斤拷锟酵拷摹锟�

锟斤拷态锟秸帮拷锟斤拷锟斤拷锟叫绑定的讹拷锟斤拷 newthis 锟斤拷锟斤拷锟斤拷值应锟斤拷锟斤拷为 NULL锟斤拷锟斤拷锟斤拷锟斤拷然锟斤拷锟斤拷锟斤拷 bubdTo 锟斤拷锟斤拷锟斤拷锟侥憋拷锟斤拷锟角碉拷锟斤拷锟斤拷锟斤拷锟斤拷

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

Note:

锟斤拷锟斤拷锟街伙拷锟斤拷锟揭拷锟斤拷锟揭伙拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟� cloning 锟斤拷锟芥。

锟斤拷锟斤拷

newthis

锟襟定革拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷一锟斤拷锟斤拷锟襟,伙拷锟斤拷 NULL 锟斤拷取锟斤拷锟襟定★拷

newscope

锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟津,伙拷锟斤拷 'static' 锟斤拷锟街碉拷前状态锟斤拷锟斤拷锟斤拷锟揭伙拷锟斤拷锟斤拷锟斤拷锟绞癸拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟轿拷牡锟斤拷锟斤拷锟斤拷锟斤拷锟� 锟斤拷锟斤拷锟斤拷锟襟定的讹拷锟斤拷锟� 锟斤拷锟斤拷锟斤拷私锟叫筹拷员 锟斤拷锟斤拷锟侥可硷拷锟皆★拷

锟斤拷锟斤拷值

锟斤拷锟斤拷锟铰达拷锟斤拷锟斤拷 Closure 锟斤拷锟斤拷 锟斤拷锟斤拷锟斤拷失锟斤拷时锟斤拷锟斤拷 FALSE

锟斤拷锟斤拷

Example #1 Closure::bindTo() 实锟斤拷

<?php

class {
    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

锟轿硷拷