Page 1 of 1

Functions, UDFs and Callback Functions in PHP

Posted: Tue Oct 05, 2010 3:27 pm
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

Re: Functions, UDFs and Callback Functions in PHP

Posted: Tue Oct 05, 2010 3:49 pm
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.

Re: Functions, UDFs and Callback Functions in PHP

Posted: Wed Oct 06, 2010 9:49 am
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