tags:

views:

172

answers:

1

How can callback functions help me in my development? Especially with PHP. I am new to php and to programming itself but I saw the power of callbacks in js libraries like jquery. I have worked with a callback in php but I was left asking a lot of questions about it:

  1. what is the scope of a callback function
  2. what parameters can I pass to a callback function (either implicitly or explicitly)

here is a code from the Kohana documentation:

$post->add_callbacks('email', array($this, '_unique_email'));

// Define the callback method
/*
 * Callback method that checks for uniqueness of email
 *
 * @param  Validation  $array   Validation object
 * @param  string      $field   name of field being validated
 */
public function _unique_email(Validation $array, $field)
{
   // check the database for existing records
   $email_exists = (bool) ORM::factory('user')->where('email', $array[$field])->count_all();

   if ($email_exists)
   {
       // add error to validation object
       $array->add_error($field, 'email_exists');
   }
}

how did this callback work? where did the parameters come from?

+1  A: 

I don't know Kohana, but I assume that that implies that when validation the email, it calls _unique_email().

In PHP callbacks can help you with development to provide you with an easy way to extend your code. For example:

$post = new Blog_Post();
$post->contents = $_POST['contents'];
$post->save();

This piece of code would create a new blog post, set the contents, and save it in the database. But, let's say you want to process BBCode in the contents. How can you do it, without having to go edit the Blog_Post class? With a callback.

$post = new Blog_Post();
$post->onSave('parseBBCode');
$post->contents = $_POST['contents'];
$post->save(); # Will call parseBBCode when saving the blog post.

function parseBBCode($contents) {
  # Parse BBCode and return the parsed contents.
}

Obviously, there are different ways of implementing callbacks, but you said you knew the power of callbacks in JS, and they are always onClick, onLoad, onKeyUp, etc, so I wanted to give an example that somewhat resembled that behavior.

I hope this was of any help. Regarding your questions about parameters, it depends on what is given to the callback. In my example, any function that is being triggered onSave will only be given the $contents parameter.

In blog_post, I have a onSave() function, which would store the function to be called back, and then when the save() function was trying to save a post, it would call any callbacks that had been defined with onSave(). I hope I'm being clear enough.

Basically, the arguments that are passed to the callbacks depend on the functions that call them. I could make the save() function call parseBBCode() with whatever arguments I wanted, but those are decided by the function that calls the callback, not by the callback. The callback has no control over what arguments are passed to it.

Bruno De Barros