ProDraz
ProDraz

Reputation: 1281

Constructor with empty return

I understand why there should be no return values in constuctor, because it will not return any.

But still I see exactly this in a lot of scripts on the internet and I haven't managed to find an answer and it's buggin me for a while. If anyone would be kind enough to explain me, I would really apreciate.

Code I am refering to:

function __construct() {

    return;
}

Upvotes: 0

Views: 843

Answers (2)

The Alpha
The Alpha

Reputation: 146201

Actually, you may (no error will be occurred) use the return keyword in the __construct method but (you don't) that would be useless and won't return anything.

The __construct method doesn't create an object instance

It's not true that, the constructor creates the object instead, it's the new keyword which creates an object instance and returns the object. So, when we use something like this:

$obj = new SomeClass;

Then PHP creates the object returns the object instance. If this class contains any constructor method then this method is called by PHP after creation of that object instance. So, you can imagine, something similar:

class SomeClass {
    public $prop = null;
    public function init() {
        $this->prop = 'SomeValue';
    }
}

Now create the object and call the init method manually

$obj = new SomeClass;
$obj->init();

We created an instance of SomeClass and called the init methos manually but in the case of __construct method PHP calls the method automatically just after the creation of that object. So, we don't have to manually set the value to the prop property.

So, before the __construct method is called the object instance is created and without creating the object instance, the __construct method couldn't be called by PHP because $this refers to current object which is already created and if the object is not initialized then how can we call:

public function __construct()
{
    $this->prop = 'SomeValue'; // $this refers to current object
}

The new keyword always returns an object instance:

Let's see this example:

class SomeClass {
    public $prop = null;
    public function __construct() {
        $this->prop = 'SomeValue';
        return $this->prop; // won't work
    }
    
    public function getProp() { ...  }
}

Now check this:

$obj = new SomeClass;

If we were able to return a value from the constructor then how could we use the object instance using something like $obj->getProp() because logically it would contain SomeValue instead of the object reference/identifier, so it makes sense that PHP should not allow to return anything from the __construct method because this magic method get called automatically just after the creation of the object and we use new keyword to create the object instance that returned to us (into a variable) by the new keyword and if we return anything from the __construct method, it prevented by PHP and only an instance of the created object is returned if we didn't throw an exception explicitly.

To create an instance of a class, the new keyword must be used. An object will always be created unless the object has a constructor defined that throws an exception on error. Reference:PHP Manual

Though it's possible to use the return in the __constructor but it would be meaningless for example, check this:

class SomeClass {
    
    public function __construct() {
        return 'SomeValue'; // won't work
        // or blank return
        return;
    }
}

Perhaps, you may do this (only for example):

class SomeClass {
    
    public function __construct() {
        if('some condition does not met then ...') return;
        $this->prop = 'Prop is set';
    }

    public function doSomething(){
        return $this->prop ? $this->prop : 'Sorry! Prop is set.';
}
}

Now try this:

$obj = new SomeClass;
echo $obj->doSomething();

If we just return before the second line in the constructor then the property won't be set otherwise the property will be set and doSomething method will output Prop is set. This may not a good way but for the example, I think this could be a valid code where return will stop execution of rest of the code. But it's not a good idea and maybe not required but for he sake of the example I can think the use of return, otherwise there is no need to use the return keyword in constructor and if someone uses it then it could be a personal choice of style or I don't know why.

Upvotes: 2

tf.alves
tf.alves

Reputation: 917

That usually is a sign of poor implementation. A constructor "returns" nothing but an instance of it's own class.

Upvotes: 0

Related Questions