Closure::bindTo

(PHP 5 >= 5.4.0, PHP 7)

Closure::bindTo Дублирует замыкание с указанием связанного объекта и области видимости класса

Описание

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

Создает и возвращает новую анонимную функцию с тем же телом функции и связанными переменными, но с другим связанным объектом или новой областью видимости класса.

"Привязанный объект" определяет значение $this, которое будет доступно в теле функции, а "область видимости класса" представляет собой класс, который определяет к каким protected (защищенным) и private (закрытым) элементам этого объекта будет иметь доступ анонимная функция. Если точнее, то это те элементы, как если бы анонимная функция была бы методом класса, переданного в параметре newscope.

Статические замыкания не могут иметь привязанный объект (значение параметра newthis должно быть равно NULL), но эта функция может все равно использоваться для изменения его области видимости класса.

Данный метод гарантирует, что у нестатического замыкания с привязанным объектом будет задана область видимости и наоборот. Для выполнения этого условия применяются следующие правила: Для нестатического замыкания, с указанной областью видимости и с NULL вместо объекта, будет создано статическое замыкание. Для нестатического замыкания с незаданной областью видимости, но с указанием объекта, создается замыкание с неуказанной областью видимости.

Замечание:

Если вам необходимо только дублировать анонимную функцию, то вы можете вместо данного метода использовать клонирование.

Список параметров

newthis

Объект, к которому будет привязана переданная функция, или NULL для отсоединения функции от ее текущего объекта.

newscope

Область видимости класса, с которой ассоциируется замыкание, или 'static' для сохранения текущей области видимости. Если передан объект, то будет использован его класс. Этот параметр определяет видимость protected (защищенных) и private (закрытых) методов привязанного объекта.

Возвращаемые значения

Возвращает новый объект Closure или FALSE в случае возникновения ошибки

Примеры

Пример #1 Пример Closure::bindTo()

<?php

class {
    function 
__construct($val) {
        
$this->val $val;
    }
    function 
getClosure() {
        
//Возвращает замыкание, связанное с текущими объектом и областью видимости
        
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

Смотрите также

Коментарии

With rebindable $this at hand it's possible to do evil stuff:

<?php
   
class {
        private 
$a 12;
        private function 
getA () {
            return 
$this->a;
        }
    }
    class 
{
        private 
$b 34;
        private function 
getB () {
            return 
$this->b;
        }
    }
   
$a = new A();
   
$b = new B();
   
$c = function () {
        if (
property_exists($this"a") && method_exists($this"getA")) {
           
$this->a++;
            return 
$this->getA();
        }
        if (
property_exists($this"b") && method_exists($this"getB")) {
           
$this->b++;
            return 
$this->getB();
        }
    };
   
$ca $c->bindTo($a$a);
   
$cb $c->bindTo($b$b);
    echo 
$ca(), "\n"// => 13
   
echo $cb(), "\n"// => 35
?>
2011-12-18 18:23:48
http://php5.kiev.ua/manual/ru/closure.bindto.html
Автор:
Closures can rebind their $this variable, but private/protected methods and functions of $this are not accessible to the closures. 

<?php
$fn 
= function(){
    return 
$this->foo;
};

class 
Bar{
    private 
$foo 3;
}

$bar = new Bar();

$fn $fn->bindTo($bar);

echo 
$fn(); // Fatal error: Cannot access private property Bar::$foo
2012-03-03 03:00:43
http://php5.kiev.ua/manual/ru/closure.bindto.html
Private/protected members are accessible if you set the "newscope" argument (as the manual says).

<?php
$fn 
= function(){
    return ++
$this->foo// increase the value
};

class 
Bar{
    private 
$foo 1// initial value
}

$bar = new Bar();

$fn1 $fn->bindTo($bar'Bar'); // specify class name
$fn2 $fn->bindTo($bar$bar); // or object

echo $fn1(); // 2
echo $fn2(); // 3
2012-03-09 15:35:02
http://php5.kiev.ua/manual/ru/closure.bindto.html
You can do pretty Javascript-like things with objects using closure binding:

<?php
trait DynamicDefinition {
   
    public function 
__call($name$args) {
        if (
is_callable($this->$name)) {
            return 
call_user_func($this->$name$args);
        }
        else {
            throw new 
\RuntimeException("Method {$name} does not exist");
        }
    }
   
    public function 
__set($name$value) {
       
$this->$name is_callable($value)? 
           
$value->bindTo($this$this): 
           
$value;
    }
}

class 
Foo {
    use 
DynamicDefinition;
    private 
$privateValue 'I am private';
}

$foo = new Foo;
$foo->bar = function() {
    return 
$this->privateValue;
};

// prints 'I am private'
print $foo->bar();

?>
2013-02-14 11:30:38
http://php5.kiev.ua/manual/ru/closure.bindto.html
Автор:
We can use the concept of bindTo to write a very small Template Engine:

#############
index.php
############

<?php

class Article{
    private 
$title "This is an article";
}

class 
Post{
    private 
$title "This is a post";
}

class 
Template{

    function 
render($context$tpl){

       
$closure = function($tpl){
           
ob_start();
            include 
$tpl;
            return 
ob_end_flush();
        };

       
$closure $closure->bindTo($context$context);
       
$closure($tpl);

    }

}

$art = new Article();
$post = new Post();
$template = new Template();

$template->render($art'tpl.php');
$template->render($post'tpl.php');
?>

#############
tpl.php
############
<h1><?php echo $this->title;?></h1>
2015-01-19 12:40:04
http://php5.kiev.ua/manual/ru/closure.bindto.html
Access private members of parent classes; playing with the scopes:
<?PHP
class Grandparents{ private $__status1 'married'; }
class 
Parents extends Grandparents{ private $__status2 'divorced'; }
class 
Me extends Parents{ private $__status3 'single'; }

$status1_3 = function()
{
   
$this->__status1 'happy';
   
$this->__status2 'happy';
   
$this->__status3 'happy';
};

$status1_2 = function()
{
   
$this->__status1 'happy';
   
$this->__status2 'happy';
};

// test 1:
$c $status1_3->bindTo($R = new MeParents::class);           
#$c();    // Fatal: Cannot access private property Me::$__status3

// test 2:
$d $status1_2->bindTo($R = new MeParents::class);
$d();
var_dump($R);
/*
object(Me)#5 (4) {
  ["__status3":"Me":private]=>
  string(6) "single"
  ["__status2":"Parents":private]=>
  string(5) "happy"
  ["__status1":"Grandparents":private]=>
  string(7) "married"
  ["__status1"]=>
  string(5) "happy"
}
*/

// test 3:
$e $status1_3->bindTo($R = new MeGrandparents::class);   
#$e(); // Fatal: Cannot access private property Me::$__status3

// test 4:
$f $status1_2->bindTo($R = new MeGrandparents::class);   
$f();
var_dump($R);
/*
object(Me)#9 (4) {
  ["__status3":"Me":private]=>
  string(6) "single"
  ["__status2":"Parents":private]=>
  string(8) "divorced"
  ["__status1":"Grandparents":private]=>
  string(5) "happy"
  ["__status2"]=>
  string(5) "happy"
}
*/
?>

Clear the stack trace:
<?PHP
use Exception;
use 
ReflectionException;

$c = function()
{
   
$this->trace = [];
};

$c $c->bindTo($R = new ReflectionExceptionException::class);
$c();

try
{
    throw 
$R;
}
catch(
ReflectionException $R)
{
   
var_dump($R->getTrace());
}
/*
array(0) {
}
*/
?>
2016-05-31 07:44:36
http://php5.kiev.ua/manual/ru/closure.bindto.html
Автор:
If you want to unbind completely the closure and the scope you need to set both to null:

<?php
class MyClass
{
    public 
$foo 'a';
    protected 
$bar 'b';
    private 
$baz 'c';

   
/**
     * @return array
     */
   
public function toArray()
    {
       
// Only public variables
       
return (function ($obj) {
            return 
get_object_vars($obj);
        })->
bindTo(nullnull)($this);
    }
}
?>

In this example, only the public variables of the class are exported (foo).

If you use the default scope (->bindTo(null)) also protected and private variables are exported (foo, bar and baz).

It was hard to figure it out because there is nowhere mentioned in the documentation that you can use null as a scope.
2018-04-19 18:31:08
http://php5.kiev.ua/manual/ru/closure.bindto.html
Автор:
Get all object vars without using Reflection:

<?php

declare(strict_types=1);

class 
A
{
    private 
$foo 'foo';
    protected 
$bar 'bar';
    public 
$buz 'buz';
}

function 
get_object_vars_all($object): array
{
    if (!
\is_object($object)) {
        throw new 
\InvalidArgumentException(sprintf('The argument should be an object, "%s" given.'get_debug_type($object)));
    }

   
$closure = function () {
        return 
get_object_vars($this);
    };

    return 
$closure->bindTo($object$object)();
}

$a = new A();

var_dump(get_object_vars($a));
var_dump(get_object_vars_all($a));

?>

The output: 

array(1) {
  ["buz"]=>
  string(3) "buz"
}
array(3) {
  ["foo"]=>
  string(3) "foo"
  ["bar"]=>
  string(3) "bar"
  ["buz"]=>
  string(3) "buz"
}
2022-02-12 13:56:39
http://php5.kiev.ua/manual/ru/closure.bindto.html
If you, like me, did not immediately understand what exactly "(an object of) an internal class" in the documentation about the 'newScope' parameter:

The documentation is about the object of the Closure itself:

<?php

class {}

$a = new A();

$closure = fn() => null;

$binded $closure->bindTo($a$closure,); // Warning: Cannot bind closure to scope of internal class Closure
2023-11-20 21:09:40
http://php5.kiev.ua/manual/ru/closure.bindto.html

    Поддержать сайт на родительском проекте КГБ