title: PHP function param type best practices
body: <p>I am currently working on a framework and have come accros a snag... how should I handle incorrect parameter types when someone calls a function in the framework?</p>
  
  <p>Example:</p>
  
  <pre><code>// Title is expected to be string, comment_num is expected to be int
  function example1($title, $comment_num) {
  
  
   // Doesnt throw error, just converts type 
   $title = (string) $title;
   $comment_num = (int) $comment_num;
  
  }
  </code></pre>
  
  <p>or</p>
  
  <pre><code>// Title is expected to be string, comment_num is expected to be int
  
  function example2($title, $comment_num) {
  
  
   if (!is_string($title)) {
  
    trigger_error('String expected for first parameter', E_USER_WARNING);
    return;
   }
  
   if (!is_string($title)) {
  
    trigger_error('Int expected for second parameter', E_USER_WARNING);
    return
   }
  }
  </code></pre>
  
  <p>Or would a mixture of both work? Throw an error and convert the type anyway?</p>
  
  <p>What would be the best way of doing this? I plan on releasing it so it's not just going to be me using it, therefore I want to think of the best way for others as well. Thanks.</p>
  
  <p>EDIT!!!</p>
  
  <p>So I decided to give the answer but i also wanted to post the code i made which allows me to quickly check types. Its abit rough but it works well enough.</p>
  
  <pre><code>function __type_check($params) {
  
      if (count($params) &lt; 1) {
  
          return; 
      }
      $types = func_get_args();
      array_shift($types);
  
      $backtrace = debug_backtrace();
      $backtrace = $backtrace[1];
  
      $global_types = array(
          'bool'  =&gt; 'boolean',
          'int'   =&gt; 'integer',
          'float' =&gt; 'double' 
      );
  
      $error = false;
  
  
      for ($i = 0, $j = count($types); $i &lt; $j; ++$i) {
  
          if (strpos($types[$i], ',') === false) {
  
              $type = strtolower($types[$i]);
  
              if (isset($global_types[$type])) {
  
                  $type = $global_types[$type];
              }
  
              if (gettype($params[$i]) != $type) {
                  $error = true;
                  break;
              }
  
          } else {
  
              $current_types = array_map('trim', explode(',', $types[$i]));
  
              foreach ($current_types as $type) {
  
                  $type = strtolower($type);  
  
                  if (isset($global_types[$type])) {
  
                      $type = $global_types[$type];
                  }
  
                  if (gettype($params[$i]) == $type) {
  
                      continue 2; 
                  }
              }
  
              $error = true;
              break;
          }       
      }
  
      if ($error) {
          trigger_error($backtrace['function'] . '() expects parameter ' . ($i + 1) . ' to be ' . $types[$i] . ', ' . gettype($params[$i]) . ' given', E_USER_WARNING);
          return false;
      }
  
      return true;
  }
  </code></pre>
  
  <p>And you would use it like this:</p>
  
  <pre><code>function string_manipulation($str, $str2, $offset = 1) {
  
      if (!__type_check(func_get_args(), 'string', 'string', 'int,float')) {
  
          return false;   
      }   
  
      // do manipulation here
  }
  </code></pre>
  
  <p>That would basically check that the first and second parameters are strings, and the 3rd parameter is an integer or a float. You can combine any types 'string,int,array,object' etc and all valid types are taken from gettype</p>
  
  <p>/* Known bugs */
  null is a type, cant decide on if it should be or not
  if you enter a class name, it doesn't check instance of but just does typecheck
  havent figured out a good way to trigger the error... meh</p>
  
  <p>Thats it from me, the bugs can be easily fixed :D</p>