xpavlus
xpavlus

Reputation: 11

Two returns in a row

While reviewing someone's code, I meet two return in a row in several classes. For example:

class class1{
    private $property1;
    final function __construct($property1){    
        $this->property1 = $property1;
    }
    private $property2 = true;
    function method1($bool){
       $this->property2 = $bool;
       return $this;
       return new class1();
    }

How does it work and what is this construction for?

Upvotes: 0

Views: 63

Answers (1)

Zenel Rrushi
Zenel Rrushi

Reputation: 2366

PHP only allows one return statement. Everything after the first return will be ignored. In you case the return new class1(); will never be called.

From PHP documentation:

If called from within a function, the return statement immediately ends execution of the current function, and returns its argument as the value of the function call. return also ends the execution of an eval() statement or script file.

You can check more here at php.net

Upvotes: 5

Related Questions