开发者

working of callback function

while reading abt array_filter() from php manual,came to face example to demostrate

the same function using callback function as given below

<?php  
function odd($var)  
{  
    // returns whether the input integer is odd  
    return($var & 1);  
}  

function even($var)  
{  
    // returns whether the input integer is even  
    return(!(开发者_开发知识库$var & 1));  
}

$array1 = array("a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5);  
$array2 = array(6, 7, 8, 9, 10, 11, 12);  

echo "Odd :\n";  
print_r(array_filter($array1, "odd"));  
echo "Even:\n";  
print_r(array_filter($array2, "even"));  
?>  

can you please help me to know how actually calback function calling,actual parameter pass,working? any link to demostrate about callback wld be great help.


In the two given examples, array_filter will go over the values in the passed array and send each value in it to the callback function (odd or even). The callback function will then inspect the value to see whether it is odd or even and return TRUE or FALSE. If it returns FALSE, the value is filtered from the array.

The easiest way to find out what your function is passing to your callback is to supply a callback that prints the passed arguments, e.g.

array_filter($anArray, function() { var_dump(func_get_args()) });

Callbacks are described in detail at

  • http://de.php.net/manual/en/language.pseudo-types.php#language.types.callback


Imagine you have a function like this:

function getNumbersDivisibleBy3($arr)
{
    $threes = array();
    foreach($arr as $val)
    {
       if($val % 3 == 0)
       {
           $threes[] = $val;
       }
    }
    return $threes
}

This function filters out all the numbers divisible by three from an array and returns them as another array.

Now imagine another function:

function GetWordsStartingWithC($arr)
{
    $cs = array();
    foreach($arr as $word)
    {
        if($word[0] == 'C')
        {
            $cs[] = $word;
        }
    }
    return $cs;
}

This function filters out all the words that start with C from an array of words and returns them another array.

If you look at the above functions, their meta function (as it were) can be explained as "This functions filters out all the items in an array that satisfies a condition and returns them as another array."

So instead of having to write the same boiler plate code to iterate through a list and filter out all elements that match, the PHP developers have written a function that takes an array and a string that is a function name.

In other languages, such as C#, instead of a string that is a function name, you actually pass in an object called a delegate, which is a pointer or reference to a function. But in PHP they have ways of figuring out which function you mean by the name you pass in. I don't know what those are.

So the array_filter function could look something like this (it won't as it's probably not written in PHP)

function array_filter($arr, $callbackname)
{
    $result = array();
    foreach($arr as $item)
    {
        if(call_user_func($callbackname, $item))
        {
             $result[] = $item;
        }
    }
    return $result;
}

In that function you can see how similar it is to the previous two, but instead of a predefined condition, it calls back (using the call_user_func() function) the function to be used via the name you passed in and applies it to each item in the array by using each item as a parameter for the function.

So you can reduce the amount of code you write by using array_filter because you don't have to write the boiler plate iteration code, just the conditional function you need to filter on.


A callback function is a function that it "called back" by another one. In the case of array_filter() the callback is invoked for every element of the array, and that element is the argument passed to it. You don't control what argument are passed, that's up the the main function you're using.

So you basically say to array_filter(): please run through this array and apply this function named "odd" to every element; this function will return a boolean value so you know what to keep and what to discard.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜