Advertisement
Guest User

Callable and callbacks.

a guest
Jun 8th, 2011
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.01 KB | None | 0 0
  1. <?php
  2. class CurrentTime {
  3.     public function __invoke($format) {
  4.         return 'Invoked : ' . date($format);
  5.     }
  6.    
  7.     public static function getCurrentDateTime($format) {
  8.         return 'Func array : ' . date($format);
  9.     }
  10. }
  11.  
  12. $invokable = new CurrentTime();
  13.  
  14. $function = function($format) {
  15.     return 'Closure : ' . date($format);
  16. };
  17.  
  18. $funcarray = array('CurrentTime', 'getCurrentDateTime');
  19.  
  20. function examineCallbacks($callbacktype, $callback) {
  21.     echo gettype($callback), ' is ', (!is_callable($callback) ? 'not ' : ''), 'callable', PHP_EOL;
  22.     if (is_callable($callback) && !is_array($callback)) {
  23.         echo $callback('r'), PHP_EOL;
  24.  
  25.     } else if (is_array($callback)) {
  26.         echo 'Handling ', $callbacktype, ' via call_user_func', PHP_EOL, call_user_func($callback, 'r'), PHP_EOL;
  27.     }
  28. }
  29.  
  30. $string = 'date';
  31.  
  32. examineCallbacks('Invokable', $invokable);
  33. examineCallbacks('Closure', $function);
  34. examineCallbacks('String', $string);
  35.  
  36. examineCallbacks('Array', $funcarray);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement