| 
	
 | 
 Posted by Dikkie Dik on 06/18/30 11:32 
What do you WANT your function to return? If (presuming PHP5 here) there  
is a real error and the function cannot reasoable be expected to return  
anything, throw an exception. 
 
An exception communicates: "This is not my responsibility". It is the  
same situation like in a company, an employee hands back a task to his  
boss, saying "I cannot do this in the current situation". 
It is now the calling code that can take action (with a catch block) or  
step aside and let the exception pass to its calling code. 
If I would have to find a better name for an exception,I would have  
called it an "objection". Like in the american court-of-law TV series. 
If someone raises an objection, the judge (the calling code) decides:  
forget the question, ask the question differently, ask it to someone  
else, or retry and ask again. 
 
It COULD also be that you want your function to return a default (a NULL  
object). Suppose you write an application involving customer objects.  
You have some function (method of a collection class) that takes the  
customer number and returns the customer with that number from the  
database. Now a new customer uses the application and no customer number  
is given. You could have your lookup function return a new blank  
customer object. 
 
Look at what the requesting code does after it receives the FALSE or  
NULL value. If it always creates some new, blank structure, have it  
returned by the lookup function. 
 
Best regards. 
 
Thomas Mlynarczyk wrote: 
> Hi, 
>  
> It seems to be a generally adopted convention to have a function return 
> FALSE in case of an error. But if a function is supposed to return a boolean 
> anyway, one cannot distinguish anymore between the "normal" FALSE and the 
> "error" FALSE. So why not using NULL instead to indicate an error? Are there 
> drawbacks I am not aware of? 
>  
> Greetings, 
> Thomas 
>  
>
 
  
Navigation:
[Reply to this message] 
 |