Hook, callback

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
When a read php source code I ofen find words like "hook" and
"callback" (in comments). I trided to search this group for these
words, but found nothing (or one hit).

What is an hook and what is a callback? Can someone give a simple
explanation, or an example on how to implement it.

In case hooks and callbacks isn't related, I am just asking what a
callback is, in this tread.

OK, I found it (callback) with my search engine, but can you guys
explain it to me in a simple manner?

Re: Hook, callback

Quoted text here. Click to load it

I found these data in a link:

// This function uses a callback function.
function doIt($callback)
    $data = "this is my 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.

Displays: Data is: this is my data

<http://stackoverflow.com/questions/48947/how-do-i-implement-a -
callback-in-php> Can someone give an "real" example, or just an exampe
with another wording?

To me this is very hard to understand, because they use the same words
in different plases.

Re: Hook, callback

Roy A. escribió:
Quoted text here. Click to load it

Probably it's not a fully exact definition but a callback in PHP is
basically a function name you pass as an argument to another function.
This second function will call the first function internally to perform
certain tasks. For example:


function english_format($number){
    return number_format($number, 2, '.', ',');
function spanish_format($number){
    return number_format($number, 2, ',', '.');

function print_data($data, $format_callback){
    foreach($data as $i){
        echo $format_callback($i) . "\n";

$data = array(1234.567, 98765.432);

print_data($data, 'english_format');
print_data($data, 'spanish_format');


Prints this:


A hook is a technique to build extensible code. For instance, you can
write a plug-in API by providing hooks at points where it can be useful.
When your code is about to do something it runs the hooks registered by
plugins. It relates to callbacks because that's one of the methods you
can use to define hooks.

-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com

Re: Hook, callback

On 13 Aug, 08:36, "=C1lvaro G. Vicario"
Quoted text here. Click to load it

Ok. This seems easy, maybe that's why I'm confused. In this examle,
you're is just treating the function as if it was a varibable.

$format_callback = 'english_format';

is the same as:


Quoted text here. Click to load it

So if I in a system can create a callback function prefixed with a
module name, eg. "modulename_beforeContent( $whatever )", all my
system have to do is to register my module name in a database table,
load my module/plugin file, an then check if there is any function
named mymoodulename_beforeContent, and if there is, run it. Maybe even
pass me some variables I might have use for.

Thank you. I think I got it.

Re: Hook, callback

Roy A. wrote:
Quoted text here. Click to load it

It's a very simple example, but one which shows how it works..  More
often than not, callbacks are used in conjunction with other things -
for instance, reading a file one line at a time and performing an action
on each line.  Or, items in an array, such as usort() does.

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Re: Hook, callback

Quoted text here. Click to load it

I don't have any examples for you, but the wikipedia definition of
Hooking ( http://en.wikipedia.org/wiki/Hooking ) says that:

"In computer programming, the term hooking covers a range of
techniques used to alter or augment the behavior of an operating
system, of applications, or of other software components by
intercepting function calls or messages or events passed between
software components. Code that handles such intercepted function
calls, events or messages is called a "hook".

Hooking is used for many purposes, including debugging and extending
functionality. Examples might include intercepting keyboard or mouse
event messages before they reach an application, or intercepting
operating system calls in order to monitor behavior or modify the
function of an application of other component.

Hooking can also be used by malicious code. For example, rootkits,
pieces of software that try to make themselves invisible by faking the
output of API calls that would otherwise reveal their existence, often
use hooking techniques. A wallhack is another example of malicious
behavior that can stem from hooking techniques. It is done by
intercepting function calls and altering what is shown to the player."

So, say you had a php script where you called a particular function a
bunch of times. You are having some problems, and you want to debug
it. With hooking, you can intercept the call to the function before it
happens, just after it happens, etc. It's an interruption of the
normal programming flow, where you would add functionality just before
or after, or within, the function call.

But, I couldn't find any examples of hooks in php. Sorry!

Re: Hook, callback

Quoted text here. Click to load it

I'm thinking that hooks are used a lot on open source code.  There are a lot of
hooks in the code for smf (simple
machine forums).  They use them to allow for integration with the rest of a PHP

Like, in the registration process there is a hook that you can use to extend the
registration to another area, so when
the person registers for the forum he will also be registered for the site
itself (on a different database).  I just did
this but unfortunately from the other side, ie I extended my site code so
registration would include my smf

Site Timeline