回调函数如何帮助我的开发?尤其是 PHP。我是 php 和编程本身的新手,但我在 jquery 等 js 库中看到了回调的强大功能。我在 php 中使用过回调,但我被问了很多关于它的问题:
- 回调函数的范围是什么
- 我可以将哪些参数传递给回调函数(隐式或显式)
这是来自 Kohana 文档的代码:
$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');
}
}
这个回调是如何工作的?参数从何而来?