p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: Should I "typecast" functions? How should I do it in PHP?


Message #1 by "Dan Ostrowski" <dan@t...> on Thu, 30 May 2002 22:07:57
Heres the deal:  I think I want to start putting a "type" designator in 
front of functions for two reasons:

1) it will keep me from getting confused on what type of output i should 
be getting from functions... i can look at a function quickly and know 
what should be 'coming out'.

2) it will (hopefully) get me in practice for C++ (which i am going to 
start in earnest after i finish the Pro PHP4 book).

is this stupid?  if not, how can you set the type for function outputs in 
PHP?

is it like:

function (type) functionName()
{
   //stuff
}





thoughts would be great,
dan
Message #2 by "Nikolai Devereaux" <yomama@u...> on Thu, 30 May 2002 14:15:01 -0700
> Heres the deal:  I think I want to start putting a "type" designator in
> front of functions for two reasons:
>
> 1) it will keep me from getting confused on what type of output i should
> be getting from functions... i can look at a function quickly and know
> what should be 'coming out'.
>
> 2) it will (hopefully) get me in practice for C++ (which i am going to
> start in earnest after i finish the Pro PHP4 book).
>
> is this stupid?  if not, how can you set the type for function outputs in
> PHP?

You can't set the type for return values -- all you can do is write the
function and use it according to your own specifications.

Plus, any single PHP function can return anything or nothing.  By anything,
I mean values of any data type, be it a primitive data type or a
user-defined object (class).  For example:

function type_return($type_name)
{
   switch($typename)
   {
       case 'string':
           return "a string.";
       case 'int':
           return 42;
       case 'float':
           return 42.0;
       case 'array':
           return array();
       case 'MyObject':
           return new MyObject();
       case 'NULL':
           return NULL;
       default:
           return;  // no return value, not even null.
   }
}


Because PHP is does not enforce typechecking, if you try to use a value of
one data type in an expression where it doesn't fit, PHP makes its best
effort at converting the variable to the type it expects.

For example, in 4 + 3.6, the integer 4 is "coerced" into a float with the
value 4.0, so that the + operator for two floating point numbers can be
applied.

Similarly, in 53 + "99 bottles of beer", the string is converted to the
integer 99, so that the + operator for two integers can be applied.


Your best bet is to keep the return values of your functions consistent.  If
I write a function that returns an array, I ALWAYS have it return an
array -- I don't return false on error... I prefer to return an empty array
or maybe NULL.

Because arrays are most often used in loops, iterating over an empty array
makes the return value for a function useful even in error conditions,
whereas if you returned NULL or false, you'd have to specifically include
extra error-checking code before your loop.


take care,

Nik

Message #3 by "Dan Ostrowski" <dan@t...> on Thu, 30 May 2002 22:43:44
As always, thanks a lot!

I will just have to put comment reminders a line above the function if i 
want to be consistent.

// (array)

function someArrayFunction()
{
   //do whatever, but return an array
}




the benefits/idiosyncracies of a "loosely typed" language i guess. =)
although i HAVE been guilty of "returning FALSE" in an array function.. i 
should fix that so it's not so sloppy...


thanks again,
dan
Message #4 by "Nikolai Devereaux" <yomama@u...> on Thu, 30 May 2002 14:54:09 -0700
> I will just have to put comment reminders a line above the function if i
> want to be consistent.
>
> // (array)
>
> function someArrayFunction()
> {
>    //do whatever, but return an array
> }

You can also use descriptive function names:

getResultArray()   // returns array
getAge()           // returns int
numResults()       // returns int
getAverageScore()  // returns float
getName()          // returns string
isAvailable()      // returns boolean


> the benefits/idiosyncracies of a "loosely typed" language i guess. =)
> although i HAVE been guilty of "returning FALSE" in an array function.. i
> should fix that so it's not so sloppy...

Again, it's just a matter of convention and preference.  There's nothing
wrong with returning false, as long as you stick to that convention.  Once
you start straying from the norm, you'll start introducing really hard to
find bugs because the code LOOKS perfectly fine, but something's still
wrong.

Take care,

nik


  Return to Index