Callback Functions In PHP

0
502
PH Callback functions

Here we look at the various cool ways we can call functions in PHP.

When you pass a function to another function, the passed function is a callback. In PHP, this is done by passing the name of the function. To understand the various ways we can create callbacks, lets look at some demo code.

First, lets start off with these simple output functions:

  1. function EchoLine($str, $html=true) {
  2.   $out = ($html) ? $str . "<br>" : $str . "\n";
  3.   echo $out;
  4. }
  5. function Hello1($arg1) {
  6.   EchoLine("Hello, $arg1");
  7. }
  8. function Hello2($arg1,$arg2) {
  9.   EchoLine("Hello, $arg1, $arg2");	
  10. }
  11. function Hello3($arg1,$arg2,$arg3) {
  12.   EchoLine("Hello, $arg1, $arg2, $arg3");	
  13. }

Now, take a look at all the different ways we can call these functions!

  1. // Traditional way
  2. function HelloWorld() {
  3.   Hello1("Hello");	
  4. }
  5. function HelloPlanet() {
  6.   Hello1("Earth");
  7. }
  8. function HelloUranus() {
  9.   Hello1("Uranus");
  10. }
  11.  
  12. $funcNames = array("HelloWorld","HelloPlanet","HelloUranus");
  13.  
  14. // Pretty cool eh!
  15. function RunFunction() {
  16.   $args = func_get_args();
  17.   if (!function_exists($args[0])) {
  18.     throw new Exception("Function " . $args[0] . " does not exist");
  19.   }
  20.   $numArgs = count($args);
  21.   switch($numArgs) {
  22.     case 0:
  23.       return($args[0]());
  24.     case 1:
  25.       return($args[0]($args[1]));
  26.     case 2:
  27.       return($args[0]($args[1],$args[2]));
  28.     case 3:
  29.       return($args[0]($args[1],$args[2],$args[3]));
  30.     case 4:
  31.       return($args[0]($args[1],$args[2],$args[3],$args[4]));
  32.    }
  33.   throw new Exception("Too many arguments");
  34. }
  35.  
  36. // Through call_user_func_array
  37. function RunCallback($callback, $args) {
  38.   return(call_user_func_array($callback, $args));
  39. }
  40.  
  41. // Through call_user_func
  42. foreach($funcNames as $funcName) {
  43.   call_user_func($funcName);
  44. }
  45.  
  46. // Through symbolic variables
  47. foreach($funcNames as $funcName) {
  48.   $funcName();
  49. }
  50.  
  51. $funcName = "Hello2";
  52. $funcName();
  53.  
  54. $funcName = "Hello2";
  55. $funcName("Mary");
  56.  
  57. $funcName = "Hello2";
  58. $funcName("Mary", "Paul");
  59.  
  60. // Notice Bill gets "clipped"
  61. $funcName = "Hello2";
  62. $funcName("Mary","George","Bill");
  63.  
  64. // Through variable arguments
  65. RunFunction("Hello1", "Joe");
  66. RunFunction("Hello2", "Joe", "Mary");
  67. RunFunction("Hello3", "Joe", "Mary", "Bob");
  68.  
  69. // Should trigger an exception for too many arguments
  70. try {
  71.   RunFunction("Hello3", "Joe", "Mary", "Bob", "Lisa");
  72. } catch (Exception $e) {
  73.   EchoLine("Exception: " . $e->getMessage());
  74. }
  75.  
  76. // Should trigger an exception because no such function exists
  77. try {
  78.   RunFunction("Foo");
  79. } catch (Exception $e) {
  80.   EchoLine("Exception: " . $e->getMessage());
  81. }
  82.  
  83. RunCallback("Hello1",array("You"));

Go ahead and copy this code and run it. Go through each one by one to understand it. Here is the output:

Hello, Hello
Hello, Earth
Hello, Uranus
Hello, Hello
Hello, Earth
Hello, Uranus
Hello, , 
Hello, Mary, 
Hello, Mary, Paul
Hello, Mary, George
Hello, Joe
Hello, Joe, Mary
Hello, Joe, Mary, Bob
Exception: Too many arguments
Exception: Function Foo does not exist
Hello, You

Of Interest

SHARE
Previous articleHTML: The Br Tag
Next articleDiv Hovers