Hello Stanislav,

Monday, January 19, 2009, 9:32:09 AM, you wrote:

> Hi!

>> It is a mess right now. You assign a closure to another method and get
>> access to the original owners private members. That is not only unexpected

> Could you give a code example? I'm not sure I understand what you mean 
> by access (or "assign closure to a method" for that matter) - if access 
> provided by the closure, it's exactly the intent of the closure. If some 
> other access, then please explain which one.

class Foo {
  private $myVeryOwnData;
  function bla {
    $this->bigMEss = function () { return $this->myVeryOwnData; }
  }
}

$o = new Foo;
$f = $o->bla;

echo $f();

class Bar {
  function baz{} {
    $this->xyz = $o->bla;  // or $f;
    // I am very sorry but I would expect the closure to rebind $this as
    // $this always points to the bound object. And how is this different?
    // If the different is that the closure is already bound and that is
    // what you want, then we need ways to do just that. And not inventing
    // new ways to deal with $this.
  }
}


$onother = new stdClass;
$another->blabla = $f;   // yet again a very unexpected $this.

If this really was to be bound to a $this at the creation, then we'd put it
into the scope binding list and not have it be handled implicitly. But then
we cannot do that with the current c level api. So instead you created
brand new rules, which unfortunately is the PHP way.

So how would I copy a closure instead of rebinding it?

I would use PHP's current established semantics for that. That is I'd do:

$this->closure = clone $closure;
$this->closure = $closure->copy();

How would I prevent automatic $this binding?

$closure = static function()....


> -- 
> Stanislav Malyshev, Zend Software Architect
> s...@zend.com   http://www.zend.com/
> (408)253-8829   MSN: s...@zend.com




Best regards,
 Marcus


-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to