return object from a service

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

return object from a service

Post by andre_c »

if a service is supposed to return a specific type of object if it finds its matching record on a table, what should it return if doesn't find a matching row?

null? false? empty object?

(maybe this should be on the theory forum)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

null or false, is what I tend to return.. depends on if an error occurs for others.

since you are talking about database records being found, I would return null, as that would tell me an empty result set. False would tell me an error.
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post by andre_c »

null it is then,

thanks
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

Post by Pyrite »

Code: Select all

return 0;
??
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

i prefer to return false... because the operation failed.

but then again, i would use a reference as parameter in which the object has to be returned

Code: Select all

function lookup(&$thingie)
{
  // lookup object
  $thingie =& something;
  
  // object was found, thus return true
  return true;
}
this way you can check if the operation succeeded... and then acces the found object, because $thingie will be a reference to it...
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post by andre_c »

hmm, using false makes me think that if it found the object it would return true. So in timvw's example, using false makes sense.
But i want the service to return an object, not a boolean, so using null seems to make more sense in this case to me now.
Post Reply