note 93418 rejected from functions.anonymous by nicobn
| From: | nicobn@php.net | Date: | Wed, 03 Nov 2010 21:45:15 +0000 |
| Subject: | note 93418 rejected from functions.anonymous by nicobn | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-173726@lists.php.net to get a copy of this message | ||
Note Submitter: Jaakko Salmela
----
<?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();
?>