Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.90 KB | None | 0 0
  1. <?php
  2.  
  3. // aliases definitions, @ is a reserved word and should be escaped
  4. // since is used as regex delimiter
  5. $aliases = array(
  6.     '^/?$' => 'Misc::home',
  7.     '/(.*)/mi' => 'Profile::home'
  8. );
  9.  
  10. foreach ($aliases as $alias => $dest) {
  11.     // cycle through aliases to find a matching one
  12.     $regex = '@' . $alias . '@';
  13.     preg_match($regex, $argv[1], $values);
  14.     if ($values) {
  15.         // we have a match, remove first part (whole matching string)
  16.         array_shift($values);
  17.         // get class and method and instance
  18.         list($className, $method) = explode('::', $dest);
  19.         $class = new $className;
  20.         // execute
  21.         call_user_func_array(array($class, $method), $values);
  22.         break;
  23.     }
  24. }
  25.  
  26. // only for testing purpose :)
  27. class Profile
  28. {
  29.  
  30.     public function home($userName)
  31.     {
  32.         echo 'Hola ' . $userName . "\n";
  33.     }
  34.  
  35. }
  36.  
  37. class Misc
  38. {
  39.  
  40.     public function home()
  41.     {
  42.         echo 'Esto es / y no recibe parametros' . "\n";
  43.     }
  44.    
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement