note 93418 added to functions.anonymous
| From: | JaakkoSalmela at osu1 dot php dot net | Date: | Wed, 09 Sep 2009 10:52:15 +0000 |
| Subject: | note 93418 added to functions.anonymous | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-160343@lists.php.net to get a copy of this message | ||
<?php
/**
* Using closures to dynamically extend objects
*
*/
class ClosureTest{
public $member = 'test';
protected $protectedMember = 'protectedtest';
public function __call($method, $args)
{
return call_user_func_array( $this->$method, $args);
}
}
$object = new ClosureTest();
$object->closure = function() use ($object){
return $object->member;
};
echo $object->closure();
//Output: 'test'
/**
* This won't work, you can not access protected/private members, which is fine.
*/
$object->closureAccessProtected = function() use ($object){
return $object->protectedMember;
};
$object->closureAccessProtected();
?>
----
Server IP: 80.81.171.181
Probable Submitter: 130.233.143.14
----
Manual Page -- http://www.php.net/manual/en/functions.anonymous.php
Edit -- https://master.php.net/note/edit/93418
Del: integrated -- https://master.php.net/note/delete/93418/integrated
Del: useless -- https://master.php.net/note/delete/93418/useless
Del: bad code -- https://master.php.net/note/delete/93418/bad+code
Del: spam -- https://master.php.net/note/delete/93418/spam
Del: non-english -- https://master.php.net/note/delete/93418/non-english
Del: in docs -- https://master.php.net/note/delete/93418/in+docs
Del: other reasons-- https://master.php.net/note/delete/93418
Reject -- https://master.php.net/note/reject/93418
Search -- https://master.php.net/manage/user-notes.php