Functions, UDFs and Callback Functions in PHP

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
devarishi
Forum Contributor
Posts: 101
Joined: Fri Feb 05, 2010 7:15 pm

Functions, UDFs and Callback Functions in PHP

Post by devarishi »

Hi,


[1] How does a Callback Function differ from an ordinary function and a User Defined Function such as strpos(), MyFunction() in PHP?


[2] When should we use them?


Source and Examples: http://php.net/manual/en/function.ob-start.php
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Functions, UDFs and Callback Functions in PHP

Post by VladSun »

A callback function is just an ordinary UDF or a "core" function. The main difference is that a callback function is not likely to be called directly by you. You just pass its name to a function and that function calls it whenever it needs it. Of course the function signature (i.e. arguments, type of arguments, return type) must match the expected callback function signature.
There are 10 types of people in this world, those who understand binary and those who don't
mkz
Forum Newbie
Posts: 11
Joined: Tue Oct 05, 2010 10:37 am

Re: Functions, UDFs and Callback Functions in PHP

Post by mkz »

To put it another way:

The word callback, as used by the PHP docs to denote the type of an argument like in the ob_start example, is a reference to a callable function or method.

The simplest form of a callback is a string containing the name of the function.

You can also supply a method of an object, in this format: array($object, 'methodname');

And, in PHP 5.3, with the introduction of anonymous functions, you can supply one of those as the value, too.

The callback type is described in the docs here: http://php.net/manual/en/language.pseudo-types.php
Post Reply