Reputation: 313
This is a philosophical question, actually. It's been a year I'm programming using PHP as a language and, as all of you know, PHP is very liberal as for the datatypes. I was wondering: given that, is it a good practice to allow methods to return different kind of values? Example: I'm used to set the variable that should be returned at a default value (usually false) and, if during the execution of the method everything goes well, the variable gets the value the successful execution has given. Disclaimer: I try to document everything using PHPDoc.
Upvotes: 2
Views: 115
Reputation: 1391
It's a common approach, at least in PHP, and it isn't a bad practice.
It can be documented without problem using the PHPDoc convention: http://manual.phpdoc.org/HTMLSmartyConverter/HandS/phpDocumentor/tutorial_tags.return.pkg.html
Upvotes: 2
Reputation: 25060
Seems like a correct approach: returning boolean false allows the method/function to return other logical "false" values such as 0 or the empty string as valid while still allowing the caller to check its result with the ===
operator.
Upvotes: 3
Reputation: 41050
I was wondering: given that, is it a good practice to allow methods to return different kind of values?
No, it's not. Not in PHP not in other languages. Better add an other method for a specific type of results.
Upvotes: 0