The Most Active and Friendliest
Affiliate Marketing Community Online!

“AdsEmpire”/  Direct Affiliate

Tip : Ways to specify callback function in php

D

dman_2007

Guest
Here's the code demonstrating various ways in which you can specify callback functions in php, i will discuss them each in my next post :

Code:
<?php
  function sort_function($a, $b)
  {
    if($a < $b)
    {
     return -1;
    }
    else if($a == $b)
    {
      return 0;
    }
    else
    {
      return 1;
    }
  }
 
  class TestClass
  {
    public function sort_method($a, $b)
    {
      if($a < $b)
      {
        return -1;
      }
      else if($a == $b)
      {
        return 0;
      }
      else
      {
        return 1;
      }
    }
    
    public static function static_sort_method($a, $b)
    {
      if($a < $b)
      {
        return -1;
      }
      else if($a == $b)
      {
        return 0;
      }
      else
      {
        return 1;
      }
    }
  }
  
  $array1 = array(3, 1, 5, 23, 11, 17, -3);
  $array2 = array(3, 1, 5, 23, 11, 17, -3);
  $array3 = array(3, 1, 5, 23, 11, 17, -3);
  $array4 = array(3, 1, 5, 23, 11, 17, -3);
  
  print_r($array1);
  echo '<br />'; 
  usort($array1, 'sort_function'); 
  print_r($array1);
  echo '<br />';
  print_r($array2);
  echo '<br />'; 
  usort($array2, array(new TestClass(), 'sort_method')); 
  print_r($array2);
  echo '<br />';  
  print_r($array3);
  echo '<br />'; 
  usort($array3, array('TestClass', 'static_sort_method')); 
  print_r($array3);
  echo '<br />'; 
  print_r($array4);
  echo '<br />'; 
  usort($array4, create_function('$a, $b', '
  if($a < $b)
  {
    return -1;
  }
  else if($a == $b)
  {
    return 0;
  }
  else
  {
    return 1;
  }')); 
  print_r($array4);
  echo '<br />'; 
?>

Output produced by the code given above :

Array ( [0] => 3 [1] => 1 [2] => 5 [3] => 23 [4] => 11 [5] => 17 [6] => -3 )
Array ( [0] => -3 [1] => 1 [2] => 3 [3] => 5 [4] => 11 [5] => 17 [6] => 23 )
Array ( [0] => 3 [1] => 1 [2] => 5 [3] => 23 [4] => 11 [5] => 17 [6] => -3 )
Array ( [0] => -3 [1] => 1 [2] => 3 [3] => 5 [4] => 11 [5] => 17 [6] => 23 )
Array ( [0] => 3 [1] => 1 [2] => 5 [3] => 23 [4] => 11 [5] => 17 [6] => -3 )
Array ( [0] => -3 [1] => 1 [2] => 3 [3] => 5 [4] => 11 [5] => 17 [6] => 23 )
Array ( [0] => 3 [1] => 1 [2] => 5 [3] => 23 [4] => 11 [5] => 17 [6] => -3 )
Array ( [0] => -3 [1] => 1 [2] => 3 [3] => 5 [4] => 11 [5] => 17 [6] => 23 )
 
First method to specify a callback function is to simply the pass the name of the function as a string.
Second method is used when you want to use an object's method as callback function. In that case, you pass a two element array as the argument, value at index 0 is the object itself and value at index 1 is the method name as string. Third method is used when you want to use a static class method as callback function. You pass a two element array here as well, value at index 0 is the class name as a string and value at index 1 is the class method name as string. Finally, you can also create anonymous callback functions by using create_function function.
 
MI
Back