glenelkins Posted December 6, 2008 Share Posted December 6, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/135832-what-really-is-the-point-in-a-callback-function/ Share on other sites More sharing options...
fook3d Posted December 6, 2008 Share Posted December 6, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/135832-what-really-is-the-point-in-a-callback-function/#findComment-708026 Share on other sites More sharing options...
glenelkins Posted December 6, 2008 Author Share Posted December 6, 2008 I think thats probably called "post back" Quote Link to comment https://forums.phpfreaks.com/topic/135832-what-really-is-the-point-in-a-callback-function/#findComment-708031 Share on other sites More sharing options...
waynew Posted December 6, 2008 Share Posted December 6, 2008 No, that's called IPN. Quote Link to comment https://forums.phpfreaks.com/topic/135832-what-really-is-the-point-in-a-callback-function/#findComment-708040 Share on other sites More sharing options...
DarkWater Posted December 6, 2008 Share Posted December 6, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/135832-what-really-is-the-point-in-a-callback-function/#findComment-708042 Share on other sites More sharing options...
waynew Posted December 6, 2008 Share Posted December 6, 2008 Also: http://en.wikipedia.org/wiki/Callback_(computer_science) Quote Link to comment https://forums.phpfreaks.com/topic/135832-what-really-is-the-point-in-a-callback-function/#findComment-708044 Share on other sites More sharing options...
glenelkins Posted December 6, 2008 Author Share Posted December 6, 2008 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" ); } Quote Link to comment https://forums.phpfreaks.com/topic/135832-what-really-is-the-point-in-a-callback-function/#findComment-708059 Share on other sites More sharing options...
DarkWater Posted December 7, 2008 Share Posted December 7, 2008 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(). Quote Link to comment https://forums.phpfreaks.com/topic/135832-what-really-is-the-point-in-a-callback-function/#findComment-708260 Share on other sites More sharing options...
glenelkins Posted December 7, 2008 Author Share Posted December 7, 2008 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" Quote Link to comment https://forums.phpfreaks.com/topic/135832-what-really-is-the-point-in-a-callback-function/#findComment-708379 Share on other sites More sharing options...
glenelkins Posted December 7, 2008 Author Share Posted December 7, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/135832-what-really-is-the-point-in-a-callback-function/#findComment-708381 Share on other sites More sharing options...
JasonLewis Posted December 7, 2008 Share Posted December 7, 2008 I always see callback functions as small, simple functions (usually) that perform a task multiple times. I only use callback functions with array_map where I use strtolower or something like that. Quote Link to comment https://forums.phpfreaks.com/topic/135832-what-really-is-the-point-in-a-callback-function/#findComment-708414 Share on other sites More sharing options...
Mchl Posted December 7, 2008 Share Posted December 7, 2008 so again i ask..the point in a callback function is? To plug in your functionality into someone else's function. That's how I see it. Quote Link to comment https://forums.phpfreaks.com/topic/135832-what-really-is-the-point-in-a-callback-function/#findComment-708423 Share on other sites More sharing options...
glenelkins Posted December 7, 2008 Author Share Posted December 7, 2008 lol..im still yet to see a practical use Quote Link to comment https://forums.phpfreaks.com/topic/135832-what-really-is-the-point-in-a-callback-function/#findComment-708518 Share on other sites More sharing options...
Mchl Posted December 7, 2008 Share Posted December 7, 2008 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). Quote Link to comment https://forums.phpfreaks.com/topic/135832-what-really-is-the-point-in-a-callback-function/#findComment-708623 Share on other sites More sharing options...
glenelkins Posted December 8, 2008 Author Share Posted December 8, 2008 Mchl ...can you explain with an example Quote Link to comment https://forums.phpfreaks.com/topic/135832-what-really-is-the-point-in-a-callback-function/#findComment-709445 Share on other sites More sharing options...
Mchl Posted December 8, 2008 Share Posted December 8, 2008 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"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/135832-what-really-is-the-point-in-a-callback-function/#findComment-709452 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.