I was talking with a friend who does most of his coding in C today and he was mentioning callback functions and said how handy they were. Most definitions on the web seemed pretty vague and most of the code was C code involving pointers. I kinda understood it but was wondering if someone could provide a good example of a callback in PHP.

Here it's especially effective. All the RSS code is in one place, and you don't need any clumsy iterators to move over the data structure (here, text tokens). The Visitor/Listener is often a very nice replacement for the Iterator pattern. It means the mechanics of iteration can stay in the data structure being traversed.

Other languages have different tricks for this. Ruby/Smalltalk/Perl can pass an anonymous block of code (PHP lambda functions are rubbish by comparison). Java can pass an anonymous inner class to avoid polluting the namespace.

For functional languages, callbacks are the foundation. It's how you get the code to the data. You will see things like "list comprehensions" (basically generators and transformers) used in the same context. Python has some support for this, but I'm not a Python developer, so hopefully someone else will elaborate.

And, may I add, javascript excells in this. Since functions are variables, they can be passed around. Gives you plenty of ammunition to shoot yourself, or tools to write some encredibly beautiful code, depending on your skills.

And, may I add, javascript excells in this. Since functions are variables, they can be passed around. Gives you plenty of ammunition to shoot yourself, or tools to write some [encredibly beautiful code, depending on your skills.

Off Topic:

Regarding functional programming, currying et al, there are some nice examples [url="http://w3future.com/html/beyondJS/"]here](http://ianhenderson.org/currying_in_javascript.html) as well. JavaScript can be a lot of fun

OK, that doesn't explain much, because it just points you at "each". They had a better article somewhere, but I couldn't find it.

I am on a bit of "replace iterator with visitor" mission at the moment at work, so this is on my brain right now.

dagfinn said:

It may be a Builder, though.

Except that I wasn't even thinking of building anything. I was thinking that the RSS reader would output, say HTML, as the events came in. I didn't even think of building up the document internally. Goes to show how even the simplest snippet of code can be interpreted completely differently without sufficient context. I should have filled out the other methods.

But then, patterns are fuzzy things best deployed subconsciously I think.

I've never written a line of Ruby code, but I would venture to say that mixins are the Ruby equivalent of Visitor.

Visitor allows you to add new operations to a class without putting them in the class itself.

lastcraft said:

Except that I wasn't even thinking of building anything. I was thinking that the RSS reader would output, say HTML, as the events came in. I didn't even think of building up the document internally. Goes to show how even the simplest snippet of code can be interpreted completely differently without sufficient context. I should have filled out the other methods.

It's "building" on the output. I admit that's far-fetched, but it may be less far-fetched than calling it Visitor.

lastcraft said:

But then, patterns are fuzzy things best deployed subconsciously I think.

Yes and no. I sometimes need to think consciously about them to learn. I keep re-reading GoF and learning something new every time.

the visited object has an accept() or similarly generic method to allow the Visitor to replace any method that might have been defined in the visited class.

The single accept() method is C++ specific, and there the function overloading is used to dispatch. The above uses a second callback instead. I would say that as long as there is a negotiation between the two tightly coupled objects to decide the actual code block to run, then it's pretty visitorish. Otherwise the Visitor pattern isn't really a pattern, it's more of an implementation.

I do agree that I have stretched it as much as the pragmatic programmers have stretched it. I kind of hope the "Prags" win, though. I find the GOF explanation is truly horrible.