Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anonymous function for a method of an object [duplicate]

Possible Duplicate:
Calling closure assigned to object property directly

Why this is not possible in PHP? I want to be able to create a function on the fly for a particular object.

$a = 'a';
$tokenMapper->tokenJoinHistories = function($a) {
   echo $a;
};
$tokenMapper->tokenJoinHistories($a);
like image 352
Martin Avatar asked Jul 21 '11 12:07

Martin


1 Answers

PHP tries to match an instance method called "tokenJoinHistories" that is not defined in the original class

You have to do instead

$anon_func = $tokenMapper->tokenJoinHistories;
$anon_func($a);

Read the documentation here especially the comment part.

like image 154
Shakti Singh Avatar answered Oct 23 '22 03:10

Shakti Singh