On Mon, 2008-01-07 at 15:19 +0100, Stefan Priebsch wrote:
> Sam Barrow schrieb:
> > Well these errors can be handled like any other, as long as they don't
> > issue a fatal.
> 
> That's exactly my point. You need to handle them. So in pidgin PHP that 
> could look something like
> 
> function foo(int $foo) {}
> 
> try
> {
>    foo($bar);
> }
> 
> catch (WhateverException $e)
> {
>    since $bar cannot be converted to int, do_whatever
> }
> 
> Is that really betten than writing
> 
> if (!is_int($bar)) do_whatever
> foo($bar)
> 
> or
> 
> function foo($foo)
> {
>    if (!is_int($foo)) do_whatever
> }

It is much more brief, and can be detected by code documenting programs.
Keep in mind that your "do_whatever" would actually be a trigger error
with an error message including the name of the function and parameter
number. 

if (!is_int($var1)) {
        trigger_error('Argument #? to function ? must be an integer.') ;
}

Without type hinting:

function foo($var1, $var2, $var3, $var4, $var5) {
  if (!is_int($var1)) {
    trigger_error('Argument #1 to function foo() must be an integer.') ;
  }
  if (!is_int($var2)) {
    trigger_error('Argument #2 to function foo() must be an integer.') ;
  }
  if (!is_int($var3)) {
    trigger_error('Argument #3 to function foo() must be an integer.') ;
  }
  if (!is_int($var4)) {
    trigger_error('Argument #4 to function foo() must be an integer.') ;
  }
  if (!is_int($var5)) {
    trigger_error('Argument #5 to function foo() must be an integer.') ;
  }
  
  return true ;
}

With type hinting, this is shortened to:

function foo(int $var1, int $var2, int $var3, int $var4, int $var5) {
  return true ;
}

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to