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.

1
  • 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 Commented Jun 27, 2018 at 21:37

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.