»
S
I
D
E
B
A
R
«
practical callback functions
October 21st, 2009 by wood

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?


Leave a Reply

»  Substance: PHP Frameworks   »  SiteMap