note 107245 added to functions.anonymous
| From: | mike at borft dot student dot utwente dot nl | Date: | Tue, 24 Jan 2012 14:46:27 +0000 |
| Subject: | note 107245 added to functions.anonymous | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-185726@lists.php.net to get a copy of this message | ||
Since it is possible to assign closures to class variables, it is a shame it is not possible to call
them directly. ie. the following does not work:
<?php
class foo {
public test;
public function __construct(){
$this->test = function($a) {
print "$a\n";
};
}
}
$f = new foo();
$f->test();
?>
However, it is possible using the magic __call function:
<?php
class foo {
public test;
public function __construct(){
$this->test = function($a) {
print "$a\n";
};
}
public function __call($method, $args){
if ( $this->{$method} instanceof Closure ) {
return call_user_func_array($this->{$method},$args);
} else {
return parent::__call($method, $args);
}
}
}
$f = new foo();
$f->test();
?>
it
Hope it helps someone ;)
----
Server IP: 69.147.83.197
Probable Submitter: 82.161.178.213
----
Manual Page -- http://www.php.net/manual/en/functions.anonymous.php
Edit -- https://master.php.net/note/edit/107245
Del: integrated -- https://master.php.net/note/delete/107245/integrated
Del: useless -- https://master.php.net/note/delete/107245/useless
Del: bad code -- https://master.php.net/note/delete/107245/bad+code
Del: spam -- https://master.php.net/note/delete/107245/spam
Del: non-english -- https://master.php.net/note/delete/107245/non-english
Del: in docs -- https://master.php.net/note/delete/107245/in+docs
Del: other reasons-- https://master.php.net/note/delete/107245
Reject -- https://master.php.net/note/reject/107245
Search -- https://master.php.net/manage/user-notes.php