Advertisement
Guest User

Untitled

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