Hi!

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

This is not a "mess" - this is *exactly* what closures are for. If you don't want this function to be able to access private variables - do not put code that accesses private variables there! That's like saying this code is a "mess":

class Foo {
  private $myVeryOwnData;
  public function getData() {
    return $this->myVeryOwnData;
  }
}

because you can do this:

$a = new Foo();
echo $a->getData();

Oh horror!! It accessed private data!! Because you explicitly told it to access it, yes. What did you expect if you provide public method to access private data?

    // 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?

I am very sorry but your expectation is wrong. There's no reason for closures to magically rebind, and if some particular language, due to limitations of its design, has this weird WTF inside, it's a problem that we don't have to carry on into PHP. If you created a closure, you got context. That's the whole point of closure. $this is part of the context. If for some reason you need WTF-closures that keep half of the context from where they were created and half from where they were called, you may create it, but regular closures shouldn't do that.
--
Stanislav Malyshev, Zend Software Architect
s...@zend.com   http://www.zend.com/
(408)253-8829   MSN: s...@zend.com

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

Reply via email to