Sampo Sarrala
Sampo Sarrala

Reputation: 4878

Is it acceptable to return in middle of class __construct()

I know that there is a lot of questions which are close to this. However, I did not manage to find answer to simple question (I am NOT asking about returning values from constructor and I think that I understand what constructors should return).

Is there any reasons to avoid using return within __construct?

Or is this fully acceptable coding style that does not break in future because of return:

class A {
    protected $tristate = null;
    function __construct() {
        // Constructor returns instance of class automatically
        // no need to `return $this`
    }
    protected function Logic() {
        return rand(0, 1) === 1;
    }
}

class B extends A {
    function __construct() {
        parent::__construct();
        if ($this->Logic()) return;
        $this->tristate = true;
    }
}

Above one is tested and it working as expected (within my development environment), it sets parent $tristate var 50/50 to NULL/TRUE but will it work in future and could there be any problems arising when returning in a middle of constructor with void return.

Another thing that came into mind is should I use return $this instead of plain return which is normally void but PHP seems to return instance anyway, answer may well be that both return $this and plain return is just as good.

Upvotes: 3

Views: 761

Answers (3)

Luca Rainone
Luca Rainone

Reputation: 16468

From doc

Constructor void __construct ([ mixed $args [, $... ]] )

and from pseudo type definition

void as a return type means that the return value is useless.

So you can return anything... but it's useless. In your case the return works for exit from function execution: it's simply allowed.

Another question is if it's a good or bad practice...

I think that the simple return ; is a good practice for save some IF statement and annoying indentation. A return with value (ie return false;), if it's useless, is a bad practice because has no sense.

Upvotes: 2

Joe
Joe

Reputation: 15802

It's a good rule of thumb to ensure that no method returns early - aside from anything else, it means that in 6 months time, when you come back to that 1500 line function that you know you shouldn't really have written, but did it anyway, you have a slightly less nightmarish time of understanding how the heck the stupid thing works :)

By using a negated logic check - if ( ! $this->Logic() ) you get the same effect as returning early, but you can now come back to the code knowing it's never going to randomly drop out of your method somewhere in the middle of it.

Of course, there's nothing stopping you doing it except the little voice saying "I know I shouldn't", but there's another voice saying "Oh go on, it'll be fine, you'll remember that!". Don't listen to the second voice, and just do it properly :P


Quick update to be clear - there are no technical reasons to avoid returning in a constructor (the new keyword forces it to return the object, and overrides what the constructor returns), there is an excellent logical reason not to do it.

Upvotes: 0

Wolfgang Stengel
Wolfgang Stengel

Reputation: 2856

There are no ramifications to this and I don't think there ever will be. You can return anything and at any time from a constructor. The return value, if any, is ignored.

Upvotes: 3

Related Questions