tags:

views:

54

answers:

2

I have noticed that most PHP-based libraries or frameworks have classes that don't explicitly return the keywords TRUE and FALSE, instead:

if(condition)
{
  $this->boolean_property = FALSE;
  return $this->boolean_property
}

does this mean anything or is it just another "purist" move which does not present any advantage over the other approach?

here is another code from an authentication library:

protected $_logged_in = false;

public function is_logged_in()
{
 if (isset($_SESSION['userdata'])
 {
  $this->_logged_in = true;
 }
 return $this->_logged_in;
}
A: 

It's still returning FALSE. It's just also setting a instance field, presumably so you can query it later. A real example might clarify things further. Where did you see this code?

Matthew Flaschen
A: 

The second example makes sense, probably the class is checking for the $_logged_in property at a latter stage.

The first one could be simplified to:

return $this->boolean_property = FALSE;

However it still seems stupid by itself, and a simple return false; would be preferable.

Alix Axel
will you give examples when "it" is not "by itself" and the approach would prove to be beneficial?
Ygam
@Ygam: The only case where it would be beneficial to store the boolean in a object property (or variable for that matter) before returning is when you need to check for the result at a latter stage.
Alix Axel