Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 427301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T19:33:55+00:00 2026-05-12T19:33:55+00:00

How can callback functions help me in my development? Especially with PHP. I am

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T19:33:55+00:00Added an answer on May 12, 2026 at 7:33 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 383k
  • Answers 383k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I'm afraid you can't do it with portlet only since… May 14, 2026 at 10:45 pm
  • Editorial Team
    Editorial Team added an answer That project has been dormant for 2 years. That is… May 14, 2026 at 10:45 pm
  • Editorial Team
    Editorial Team added an answer You may take a look at a sample ASP.NET MVC… May 14, 2026 at 10:45 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.