Reputation: 8523
I have a problem that's driving me crazy.
I have a static class where I perform some checks, this is the code:
class MyClass
{
public static function globalChecks()
{
$result = true;
$result = $result && self::checkAgency();
$result = $result && self::checkAttribs();
$result = $result && self::checkCategories();
$result = $result && self::checkDistricts();
$result = $result && self::checkTowns();
$result = $result && self::checkTypes();
$result = $result && self::checkUser();
return $result;
}
}
All of these methods are declared as public and static.
Well if I try to run it, PHP executes the first one, then it simply skips the other ones.
I tried to debug or brutally put a die()
inside the method, but it doesn't work.
If I switch the variable with the method call (ie self::method && $result
) everything works...
It seems operator precedence is involved in some way, but am I missing something?
Upvotes: 0
Views: 192
Reputation: 160191
As soon as the first false is set, short-circuiting will cause the method calls to be skipped--no reason to run them in an "and" if the first condition is false.
Upvotes: 2
Reputation: 20159
If self::checkAgency()
returns false
, $result
will be false
and because of the left-to-right evaluation of logical operators such as &&
, none of the other methods need to be evaluated as the evaluation is short-circuited.
Are you sure that this first method returns true
in some of your test cases?
Upvotes: 0
Reputation: 5605
as soon as one of your method call will return false, the && operation won't execute the second part of the expression ! since False && anything is False.
Upvotes: 2
Reputation: 160833
Why not just
public static function globalChecks()
{
return self::checkAgency()
&& self::checkAttribs()
&& self::checkCategories()
&& self::checkDistricts()
&& self::checkTowns()
&& self::checkTypes()
&& self::checkUser();
}
Upvotes: 2