1

Upgrading from PrestaShop 1.6 to 1.7, I found a change in how the developers return the module install method. Obviously, for both the old and new way, you want to return true if ALL is ok, and false 1.6:

public function install() {
    if(!$this->someFunction() || !parent::install()) 
        return false;
    return true;
}

Sometimes the other way around:

public function install() {
    if($this->someFunction() && parent::install()) 
        return true;
    return false;
}

But now in 1.7 they do it this way, and I cannot figure out how this even works:

public function install() {
    return parent::install()
        && $this->someFunction();
}

How can a function return THIS and THAT? If I was to guess, I would think that it either returns the first TRUE/FALSE and then exits, OR returns the sum of them both (but then only FALSE && FALSE would return FALSE)

Please help me understand this.

OnklMaps
  • 807
  • 8
  • 17
  • return parent::install() && $this->someFunction(); will only return true if parent:install() is true and $this->someFunction() is also true. Else it will return false – Fr0z3n7 Jun 27 '18 at 21:37

1 Answers1

2

return this && that is read as return (this && that). this and that will be evaluated to a boolean. If both are true, then it becomes return (true && true). true && true evaluates to true. So, it becomes return true.

It's Boolean Algebra in code form.

Richard
  • 528
  • 2
  • 15
  • 1
    It should be noted that this is particulary valid for PHP. So `1 && 2` evaluates to `true` in PHP. In some scripting languages, e.g. JavaScript, it works similar, however, the evaluated result is one of the input operands itself. `1 && 2` in JavaScript evaluates to `2` since 1 evaluates to true and the second operand has to be checked as well, thus that second value is returned. `1 || 2` returns `1` in JS and `true` in PHP. – Pinke Helga Jun 27 '18 at 22:02
  • A-ha! It was the missing parentheses that made the confusion for me. By adding the parentheses it now makes sense :) And to state the obvious (true && false) will off course evaluate to false. – OnklMaps Jun 27 '18 at 23:21