<p>I would like to implement something similar to a c# delegate method in PHP.  A quick word to explain what I'm trying to do overall:  I am trying to implement some asynchronous functionality. Basically, some resource-intensive calls that get queued, cached and dispatched when the underlying system gets around to it. When the asynchronous call finally receives a response I would like a callback event to be raised.</p>

<p>I am having some problems coming up with a mechanism to do callbacks in PHP.  I have come up with a method that works for now but I am unhappy with it.  Basically, it involves passing a reference to the object and the name of the method on it that will serve as the callback (taking the response as an argument) and then use eval to call the method when need be.  This is sub-optimal for a variety of reasons, is there a better way of doing this that anyone knows of? </p>

[{'answer_id': 48585, 'author': 'Nick Stinemates', 'author_id': 4960, 'author_profile': 'https://Stackoverflow.com/users/4960', 'pm_score': 2, 'selected': False, 'text': '<p>How do you feel about using the <a href="http://en.wikipedia.org/wiki/Observer_Pattern" rel="nofollow noreferrer">Observer</a> pattern? If not, you can implement a true callback this way:</p>\n\n<pre><code>// This function uses a callback function. \nfunction doIt($callback) \n{ \n    $data = "this is my data";\n    $callback($data); \n} \n\n\n// This is a sample callback function for doIt(). \nfunction myCallback($data) \n{ \n    print \'Data is: \' .  $data .  "\\n"; \n} \n\n\n// Call doIt() and pass our sample callback function\'s name. \ndoIt(\'myCallback\'); \n</code></pre>\n\n<p>Displays: Data is: this is my data</p>\n'}, {'answer_id': 48591, 'author': 'VolkerK', 'author_id': 4833, 'author_profile': 'https://Stackoverflow.com/users/4833', 'pm_score': 5, 'selected': True, 'text': '<p>(Apart from the observer pattern) you can also use <a href="http://php.net/manual/function.call-user-func.php" rel="nofollow noreferrer"><code>call_user_func()</code></a> or <a href="http://php.net/manual/function.call-user-func-array.php" rel="nofollow noreferrer"><code>call_user_func_array()</code></a>.</p>\n\n<p>If you pass an <code>array(obj, methodname)</code> as first parameter it will invoked as <code>$obj-&gt;methodname()</code>.</p>\n\n<pre><code>&lt;?php\nclass Foo {\n    public function bar($x) {\n        echo $x;\n    }\n}\n\nfunction xyz($cb) {\n    $value = rand(1,100);\n    call_user_func($cb, $value);\n}\n\n$foo = new Foo;\nxyz( array($foo, \'bar\') );\n?&gt;\n</code></pre>\n'}, {'answer_id': 3158886, 'author': 'Benjamin', 'author_id': 381220, 'author_profile': 'https://Stackoverflow.com/users/381220', 'pm_score': 2, 'selected': False, 'text': '<p>I was wondering if we could use __invoke magic method to create "kind of" first class function and thus implement a callback</p>\n\n<p>Sound something like that, for PHP 5.3</p>\n\n<pre><code>interface Callback \n{\n    public function __invoke(); \n}\n\nclass MyCallback implements Callback\n{\n    private function sayHello () { echo "Hello"; }\n    public function __invoke ()  { $this-&gt;sayHello(); } \n}\n\nclass MySecondCallback implements Callback\n{\n    private function sayThere () { echo "World"; }\n    public function __invoke ()  { $this-&gt;sayThere(); }\n}\n\nclass WhatToPrint\n{\n    protected $callbacks = array();\n    public function register (Callback $callback) \n    {\n        $this-&gt;callbacks[] = $callback;\n        return $this;\n    }\n    public function saySomething ()\n    {\n        foreach ($this-&gt;callbacks as $callback) $callback(); \n    }\n}\n\n$first_callback = new MyCallback;\n$second_callback = new MySecondCallback;\n$wrapper = new WhatToPrint;\n$wrapper-&gt;register($first_callback)-&gt;register($second_callback)-&gt;saySomething();\n</code></pre>\n\n<hr>\n\n<p>Will print HelloWorld</p>\n\n<p>Hope it\'ll help ;)</p>\n\n<p>But I\'d prefer the Controller pattern with SPL for such a feature.</p>\n'}]
['https://Stackoverflow.com/questions/48570', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5056/']