note 97297 added to functions.anonymous

From: Date: Tue, 13 Apr 2010 02:39:04 +0000
Subject: note 97297 added to functions.anonymous
Groups: php.notes 
Request: Send a blank email to php-notes+get-168184@lists.php.net to get a copy of this message
Pending a decision on what it should mean, $this is not currently (as of 5.3.2) usable within anonymous functions. There are roughly two positions (plus attempts at compromise), that can be called "early" and "late". Early: $this refers to the object in whose scope the anonymous function is constructed. <?php class Creator { public function make_anonymous() { return function() { return $this; }; } } class Caller { public $p; public function call_anonymous($f) { $this->p = $f(); } } $alpha = new Creator; $omega = new Caller; $omega->call_anonymous($alpha->create_anonymous()); // $omega->p === $alpha ?> Late: $this refers to the object in whose scope the anonymous function is called. <?php class Creator { public function make_anonymous() { return function() { return $this; }; } } class Caller { public $p; public function call_anonymous($f) { $this->p = $f(); } } $alpha = new Creator; $omega = new Caller; $omega->call_anonymous($alpha->create_anonymous()); // $omega->p === $omega ?> So until this is cleared up, $this won't work in an anonymous function. (Personally, I favour the early method; otherwise the anonymous function - and therefore its Creator - has access to all of the Caller's private properties.) ---- Server IP: 202.89.58.1 Probable Submitter: 125.237.144.87 ---- Manual Page -- http://www.php.net/manual/en/functions.anonymous.php Edit -- https://master.php.net/note/edit/97297 Del: integrated -- https://master.php.net/note/delete/97297/integrated Del: useless -- https://master.php.net/note/delete/97297/useless Del: bad code -- https://master.php.net/note/delete/97297/bad+code Del: spam -- https://master.php.net/note/delete/97297/spam Del: non-english -- https://master.php.net/note/delete/97297/non-english Del: in docs -- https://master.php.net/note/delete/97297/in+docs Del: other reasons-- https://master.php.net/note/delete/97297 Reject -- https://master.php.net/note/reject/97297 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#168184) next »