16

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.

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?

bensiu
  • 24,660
  • 56
  • 77
  • 117
George Mauer
  • 117,483
  • 131
  • 382
  • 612

3 Answers3

16

(Apart from the observer pattern) you can also use call_user_func() or call_user_func_array().

If you pass an array(obj, methodname) as first parameter it will invoked as $obj->methodname().

<?php
class Foo {
    public function bar($x) {
        echo $x;
    }
}

function xyz($cb) {
    $value = rand(1,100);
    call_user_func($cb, $value);
}

$foo = new Foo;
xyz( array($foo, 'bar') );
?>
Asmodiel
  • 1,002
  • 1
  • 12
  • 21
VolkerK
  • 95,432
  • 20
  • 163
  • 226
4

How do you feel about using the Observer pattern? If not, you can implement a true callback this way:

// This function uses a callback function. 
function doIt($callback) 
{ 
    $data = "this is my data";
    $callback($data); 
} 


// This is a sample callback function for doIt(). 
function myCallback($data) 
{ 
    print 'Data is: ' .  $data .  "\n"; 
} 


// Call doIt() and pass our sample callback function's name. 
doIt('myCallback'); 

Displays: Data is: this is my data

Nick Stinemates
  • 41,511
  • 21
  • 59
  • 60
  • 3
    Excuse me if this is a dumb question, but what are advantages of using callback principle in this case? It would be the same to call doIt(); myCallback(); functions one after another? I get the point of the call back in AJAX or delegate pattern in Objective C or any asynchronous case but here I don't follow? – luigi7up Jun 04 '11 at 17:48
  • @luigi7up You can use this to loop through $data and call the callback function for every item in $data in a for loop. This is the way repeaters work in asp when you set a ondatabound callback – SubliemeSiem Nov 13 '16 at 21:42
3

I was wondering if we could use __invoke magic method to create "kind of" first class function and thus implement a callback

Sound something like that, for PHP 5.3

interface Callback 
{
    public function __invoke(); 
}

class MyCallback implements Callback
{
    private function sayHello () { echo "Hello"; }
    public function __invoke ()  { $this->sayHello(); } 
}

class MySecondCallback implements Callback
{
    private function sayThere () { echo "World"; }
    public function __invoke ()  { $this->sayThere(); }
}

class WhatToPrint
{
    protected $callbacks = array();
    public function register (Callback $callback) 
    {
        $this->callbacks[] = $callback;
        return $this;
    }
    public function saySomething ()
    {
        foreach ($this->callbacks as $callback) $callback(); 
    }
}

$first_callback = new MyCallback;
$second_callback = new MySecondCallback;
$wrapper = new WhatToPrint;
$wrapper->register($first_callback)->register($second_callback)->saySomething();

Will print HelloWorld

Hope it'll help ;)

But I'd prefer the Controller pattern with SPL for such a feature.

Benjamin
  • 51
  • 4