blog home

be consistent with php function return type

Posted by William in backend on April 20th, 2009

PHP is a type-less language, which gives you the flexibility of, for example, return anything from a function. This, however, can trigger hard-to-find bugs if you abuse it. A better choice would be consistent with return types, where I mean if the function is supposed to return boolean, only return boolean; if it’s supposed to return a string, always return a string; if it’s supposed to return an array, always return an array.

Here are some examples that shows how it can break your code if you do not pay attention to return types.

Example 1. Disaster: return boolean true and string:

function foo ()

{

if ( some_condition() )

return “ERROR_1″;

else if (some_other_condition() )

return “ERROR_2″;

else

return true;

}

So the author of this function is trying to return some error code (in string, bad idea!), or true if everything is good. But the problem is that any string will be evaluated to be true. So if you have a caller like this:

$rtn = foo();

if ($rtn == true)

// foo is good.

else if ( $rtn == “ERROR_1″ )

// handle ERROR_1

You will have a bug here. The “else” section will never be executed, that’s because if you compare any string to boolean true with “==” operator, it is always true. You could use === to force a type check but your callers may not be aware of that. Some solution to this would be:

  • make the function return true or false only, on throw exceptions instead of returning error code
  • if you so intend to return error code, either through string, or use defined constant (a better idea), never return a boolean. For example, rather than return boolean true, return a constant RTN_OK

in other words, when  you design your function, if you decide what type it should return, always return that type.

Example 2: a function that returns an array.

For example, you have a function that tries to get some data from memcache. If no data from memcache found, get it from database, and cache it in memcache.

function get_customers( $bookid )

{

$mc_key = “bk-cust-$bookid”;

if  ( !$customers = $memcache->get( $mc_key ) )

{

// no records in memcache

$customers = array();

$result = mysql_query(”SELECT * FROM book_customers WHERE bookid=$bookid”);

while ($row = mysql_fetch_array($result))

$customers[] = $row;

$memcache->set( $mc_key, $customers ); //cache it.

}

return $customers;

}

The problem of this function is that if there is nothing found in the database initially, you will end up setting an empty array in the memcache, then when you do memcache->get, you will get it back as a boolean true instead.

2 Responses to “be consistent with php function return type”

  1. Jenzing says:

    Great One…

    I must say, its worth it! My link, http://connie11.dokyun.jp,thanks haha…

  2. Jose Stacker says:

    I have read a few of the articles on your website now, and I really like your style of blogging. I added it to my favorites site list and will be checking back soon. Please check out my site as well and let me know what you think.

Leave a Reply