Re: How to type arguments

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Richard Heyes wrote:
I'm a french student, sorry for my mail :

I want to know how can I type my functions' arguments ?

It is heavy to do :
function post($id)
{
$id=(int)$id;
//...

PHP is loosely typed so strictly (...) speaking, that would be fine.
However, you're best off making sure that what you've been given is
what you think it is. Ergo, what you've written, is what I would do.

or tu put (int) before each use...

Not necessary to put it there for each use, just the first.


also worth noting that you can type-hint as long as the type you're hinting is an object and not a primitive:

function post(SomeObject $obj)
{
  // php will effectively throw a catchable fatal error if
  // $obj is not an instance of SomeObject
}

sadly you can't:

function post(int $number)
{
  // doesn't work for primitives
  // but then php has limited primitive types :(
}

so safest bet is as you mentioned but with at least some scalar checking first

function post( $id )
{
  if( is_scalar($id) ) {
    return (int)$id;
  }
  // if you get here you're id is an object, resource
  // or array and can't be cast to scalar type.
}

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux