Jump to content

What Really Is The Point In A Callback Function?


glenelkins

Who Actually Knows Really What A Callback Function Is? TRUTH!  

6 members have voted

  1. 1. Who Actually Knows Really What A Callback Function Is? TRUTH!

    • I Do
      3
    • I Kinda Do
      1
    • Heard Of It
      1
    • No Idea Matey
      1


Recommended Posts

Hi

 

I have just picked up an interest in Callback functions. They seem to be somewhat a frightening thing to allot of people. But personally i think a callback function using the "call user function" over complicates things.

 

This code will do exactly the same thing and is so much easier:

 

function example ( $some_var, $callback ) {

    $result = $callback ( $some_var );

}

 

so what really is the point?

 

Link to comment
Share on other sites

Correct me if I am wrong, but is a "call back" not something like what paypal uses to post data back to another server and set a payment as made?

 

I have heard people call that process both "call back" and "post back"

 

Is this not what you are referring to?

Link to comment
Share on other sites

Now, how do you plan on calling that function with multiple parameters that you might not know ahead of time?  I guess you could compile a string together and eval() it, but it's slow.  Very slow.  And error-prone.  call_user_func_array() does the job just fine though.  Also, some functions like sort() and it's relatives and even preg_replace_callback() need callbacks for good reasons.

Link to comment
Share on other sites

Thats a good point! You would have to determine the number of vars first...which i see now defies the point of using a callback function.

 

There is a but!

 

You can do this:

 

function example ( $vars_array, $callback ) {

    foreach ( $vars_array as $k => $v ) {

        $result[] = $callback ( $k, $v );

    }

}

$vars_array = array ( 'number 1' => 1,'number 2' => 2 );

example ( $vars_array. 'test' );

function test( $key, $val ) {

// this function should already know what to do with the variables
return ( "Key: $key Val: $val" );

}

 

Link to comment
Share on other sites

That point made no sense.  You just called the function multiple times with different values; you didn't pass it all in at once.  Let's say you were writing for an MVC router and you needed to send extra parameters off to the proper method.  You'd need call_user_func_array().

Link to comment
Share on other sites

well my point being you could quite easily write a simple function that you pass an array of variables to...i still cannot see the point in using a callback. Thus far i have never had a situation where i have thought "oooo a callback will solve this"

Link to comment
Share on other sites

if you think about it, you could pass an array to my way of doing it right at the top of this post. the function called should already know how to deal with the array...so again i ask..the point in a callback function is?

Link to comment
Share on other sites

Imagine array sorting function. It could take in callback function to define sorting criteria.

 

You don't care what sorting algorithm is used (whether it's bubble sort, or quicksort, or intelligent sorter sort).

You just want it use your sorting criteria (for example compare geometric averages of each row).

Link to comment
Share on other sites

Sure:

 

<?php
$points = array( //array of points on a plane
1 => array(1,1),
2 => array(1,2),
3 => array(3,1),
4 => array(0,1),
5 => array(4,2),
6 => array(3,2),
7 => array(2,2),
);


function distance($x,$y) { //calculate distance of (x,y) from (0,0)
return sqrt(pow($x,2)+ pow($y,2));
}

function compare($a,$b) { //compare distances of two points from (0,0)
$ma = distance($a[0],$a[1]);
$mb = distance($b[0],$b[1]);

if ($ma == $mb) {
	return 0;
}
return ($ma < $mb) ? -1 : 1;
}

usort($points, "compare");

foreach ($points as $key => $point) {
    echo "$key: ({$point[0]},{$point[1]}) is ".round(distance($point[0],$point[1]),4)." units from (0,0)<br/>\n";
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.