note 105634 deleted from functions.anonymous by tularis

From: Date: Sat, 03 Sep 2011 16:33:46 +0000
Subject: note 105634 deleted from functions.anonymous by tularis
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-182921@lists.php.net to get a copy of this message
Note Submitter: Hayley Watson Reason: in docs ---- As already noted in the documentation, the 5.4 alpha releases reintroduce $this back into anonymous functions; note that it is bound early and refers to the object scope in which the function created it (in contrast with, say, JavaScript, where it refers to the scope in which the function is called). So <?php class test1 { private $foo = "This is Class One"; public function make_anonymous_function() { return function() { echo $this->foo; }; } } class test2 { private $foo = "This is Class Two"; public function use_anonymous_function() { $t1 = new test1; $func = $t1->make_anonymous_function(); echo $func(); } } $t2 = new test2; $t2->use_anonymous_function(); ?> outputs "This is Class One".

« previous php.notes (#182921) next »