Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 4th, 2012  |  syntax: None  |  size: 2.68 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. PHP function param type best practises
  2. // Title is expected to be string, comment_num is expected to be int
  3. function example1($title, $comment_num) {
  4.  
  5.  
  6.  // Doesnt throw error, just converts type
  7.  $title = (string) $title;
  8.  $comment_num = (int) $comment_num;
  9.  
  10. }
  11.        
  12. // Title is expected to be string, comment_num is expected to be int
  13.  
  14. function example2($title, $comment_num) {
  15.  
  16.  
  17.  if (!is_string($title)) {
  18.  
  19.   trigger_error('String expected for first parameter', E_USER_WARNING);
  20.   return;
  21.  }
  22.  
  23.  if (!is_string($title)) {
  24.  
  25.   trigger_error('Int expected for second parameter', E_USER_WARNING);
  26.   return
  27.  }
  28. }
  29.        
  30. function __type_check($params) {
  31.  
  32.     if (count($params) < 1) {
  33.  
  34.         return;
  35.     }
  36.     $types = func_get_args();
  37.     array_shift($types);
  38.  
  39.     $backtrace = debug_backtrace();
  40.     $backtrace = $backtrace[1];
  41.  
  42.     $global_types = array(
  43.         'bool'  => 'boolean',
  44.         'int'   => 'integer',
  45.         'float' => 'double'
  46.     );
  47.  
  48.     $error = false;
  49.  
  50.  
  51.     for ($i = 0, $j = count($types); $i < $j; ++$i) {
  52.  
  53.         if (strpos($types[$i], ',') === false) {
  54.  
  55.             $type = strtolower($types[$i]);
  56.  
  57.             if (isset($global_types[$type])) {
  58.  
  59.                 $type = $global_types[$type];
  60.             }
  61.  
  62.             if (gettype($params[$i]) != $type) {
  63.                 $error = true;
  64.                 break;
  65.             }
  66.  
  67.         } else {
  68.  
  69.             $current_types = array_map('trim', explode(',', $types[$i]));
  70.  
  71.             foreach ($current_types as $type) {
  72.  
  73.                 $type = strtolower($type);  
  74.  
  75.                 if (isset($global_types[$type])) {
  76.  
  77.                     $type = $global_types[$type];
  78.                 }
  79.  
  80.                 if (gettype($params[$i]) == $type) {
  81.  
  82.                     continue 2;
  83.                 }
  84.             }
  85.  
  86.             $error = true;
  87.             break;
  88.         }      
  89.     }
  90.  
  91.     if ($error) {
  92.         trigger_error($backtrace['function'] . '() expects parameter ' . ($i + 1) . ' to be ' . $types[$i] . ', ' . gettype($params[$i]) . ' given', E_USER_WARNING);
  93.         return false;
  94.     }
  95.  
  96.     return true;
  97. }
  98.        
  99. function string_manipulation($str, $str2, $offset = 1) {
  100.  
  101.     if (!__type_check(func_get_args(), 'string', 'string', 'int,float')) {
  102.  
  103.         return false;  
  104.     }  
  105.  
  106.     // do manipulation here
  107. }
  108.        
  109. function example1($title, $comment_num) {
  110.  
  111.     // do some operations that should work error-free regardless of type
  112.  
  113.     if ($result != 'something specific you expect here') {
  114.         throw new Exception('Something went wrong!');
  115.         // or
  116.         trigger_error('Something went wrong!');
  117.         return false;
  118.     }
  119.  
  120.     // continue with $result
  121. }
  122.        
  123. function example1(TitleObject $title) {
  124.     // rest assured that $title is of the right type
  125. }