Reputation: 2848
if ( $this->guardian() ) { return $this->guardian(); }<--will this execute method twice?
$guardian = $this->guardian();
if ( $guardian ) { return $guardian; }
I have a question, if I place class method inside of if and return method, will this execute method twice? (its nice and clean, but I dont want execute twice)
Upvotes: 1
Views: 50
Reputation: 942
The first option will execute the method twice, because you are calling it 2 times.
But the second option will store the result of the method in the var $guardian
.
Your second way it's the correct way, because calling a function X
more than one time when it's not needed it's not right and inefficient, this will consume a lot more of resources.
Upvotes: 3
Reputation: 37048
Yes, it will, unless it returns 0, null, false, etc. You can do
if ( $ret = $this->guardian() ) { return $ret; }
Upvotes: 2