简体   繁体   中英

Why do we need the “return true” on this method?

This data comparison should only occur, when enable has the string 1.

public function compareDates() 
{
  if ($this->data[$this->alias]['enabled'] === "1") {
    return $this->data[$this->alias]['firstPageEnterDate'] < $this->data[$this->alias]['firstPageLeaveDate'];
  } else {
    return true;
  }
}

If I don't state the return true on the else clause, the compareDates() will never "exit", regardless of the value being "1" or "bla bla".

Why is that?

"Argh! :s I got confused: why return true means "exit function execution", and return false doesn't? I mean, should return regardless of the value, be the end of the line for that function execution? I clearly missing some core concepts. Please advice.

...But if I change return true to return false on that else clause, the methods keeps being called"

return does return regardless of the value so you are correct in your assumption there.

it seems like your issue probably lies in the function that is calling compareDates(). You say that the method "keeps being called", is compareDates() being called within a loop perhaps? you will want to double check to make sure that you are correctly handling the return value of compareDates() in the calling function.

ps (on the issue of omitting the return statement from a function)

from http://www.php.net/manual/en/functions.returning-values.php

"If the return is omitted the value NULL will be returned."

** Updated **

Now that we know you are using the Cake Framework.

from http://book.cakephp.org/2.0/en/models/data-validation.html <-- all about cake framework data-validation here

[Section: Adding your own Validation Methods]

"The method should return true if the value is valid. If the validation failed, return false. The other valid return value are strings which will be shown as the error message. Returning a string means the validation failed. "

This means that your firstPageEnterDate field will only be seen as valid if you return a value that is considered true from your compareDates() function. returning false or NULL will mean that firstPageEnterDate field is invalid.

You do not even need the else since you do not do anything else if the if statement is false

You can just return true as the last statement in the function.

This allows you to check for truthyness from the return of the function otherwise if the if statement fails, your function will return undefined

It is all up to you what you return if the if statement is false, in this case you returned true

Always have a return statement at the end :),you can also rewrite like so:

public function compareDates() 
{
  if ($this->data[$this->alias]['enabled'] === "1") {
    return $this->data[$this->alias]['firstPageEnterDate'] < $this->data[$this->alias]['firstPageLeaveDate'];
  } 
    return true;
}

something like would be clearer in my opinion.

public function compareDates() 
{
  return ($this->data[$this->alias]['enabled'] != "1")  ||

    ($this->data[$this->alias]['firstPageEnterDate'] < 
      $this->data[$this->alias]['firstPageLeaveDate'];)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM