Callbacks in PHP

Recently I was working on something and I wanted to call an object method as a callback, but got confused when I realised the method had been caused statically. This was caused by my inability to RTFM and I wondered how I’d come so far without actually coming across the many and varied things you can pass into any place a callback is needed. I think we’ve all seen a function callback; something like this:

function myExcitingFunction() {
  // do something remarkable
}

call_user_func('myExcitingFunction');

You can also call methods of objects rather than just plain functions, and this is where I tripped myself up.
To do so, you pass an array with either the name of the class or an instantiated object as the first element in the array, and the name of the method as the second element. The (in hindsight, obvious) difference is that when you pass just the name of the class, the method is called statically (which is fine, as I wrote recently). If, on the other hand, you pass an instantiated object, the method gets called against the object. The distinction looks something like this:

class ObjectOfGreatUsefulness {
  public function sparkle() {
    // shiny awesome
  }
}

// static call
call_user_func(array('ObjectOfGreatUsefulness', 'sparkle'));

// dynamic call
$sparklyThing = new ObjectOfGreatUsefulness();
call_user_func(array($sparklyThing, 'sparkle'));

Hopefully this makes it clear (for me at least!) how objects fit in with callbacks in general. You can also call relative classes, using for example the parent keyword, and closures – Read The Fine Manual page, as I failed to do, for more detail :)

11 thoughts on “Callbacks in PHP

  1. Ah yes, I have used the static version of this to get round the problem of not having late static binding available in 5.2.

  2. Nice write-up. No worries about not RTFM…I think we all get bitten by that one every now and again :)

    One additional note on what you can pass to call_user_func. You can also pass in an anonymous function, which is to say, you can pass in a lambda or closure.

  3. I’ve introduced Gearman into a project I’m working on, and since a few people have asked I thought I’d share my experiences. Basically, this application generates some PDFs from a variety of data sources, makes images, and emails it. Since the whole dat

  4. Pingback: Programowanie w PHP » Blog Archive » Lorna Mitchell’s Blog: Callbacks in PHP

  5. This is a very good article, I especially enjoyed the code

    [code]
    $sparklyThing = new ObjectOfGreatUsefulness();
    call_user_func(array($sparklyThing, ‘sparkle’));
    [/code]

    Have you ever thought of / tried having callbacks registered with an object fulfilling the observer pattern? it can be used to provide application-wide events and object events. It becomes especially useful when combined with other design patterns.

Leave a Reply

Please use [code] and [/code] around any source code you wish to share.

This site uses Akismet to reduce spam. Learn how your comment data is processed.