On Sat, 08 Oct 2011 11:02:57 +0100, Frédéric Hardy
<frederic.ha...@mageekbox.net> wrote:
Hello !
I don't think so, but if I had to summarize the innovations in 5.4,
this would be it:
- Closures can now have an associated scope
- Closures can now have a bound object
- Closures can now be either static or non-static
- Closures defined in a place with an active scope are scoped
accordingly
- Closures defined inside an instance method (or bound closure) have a
bound object (namely $this)
- Closures are static if defined within a static method or with the
static keyword (static function() { ... })
Is it possible to have sample code about these ?
They will be very interesting to understand the RFC correctly and write
documentation or tutorial.
For example, i was tryed to use $this in a closure with PHP 5.B beta
without any success :/, so if using $this in a closure is possible, i
would like to know how to do it..
You can take a look at the tests. For instance, this patch has many:
http://nebm.ist.utl.pt/~glopes/misc/closures_scope.patch
For more, you can look at the SVN tree (Zend/tests/closure_*).
In any case, here is a very simple example:
<?php
class A {
private $foo = "bar";
function foo() { return function () { return $this->foo; }; }
}
$a = new A;
$c = $a->foo();
echo $c(); //echos "bar"
--
Gustavo Lopes
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php