14

Just wondering is it best to define an empty constructor or leave the constructor definition out completely in PHP? I have a habit of defining constructors with just return true;, even if I don't need the constructor to do anything - just for completion reasons.

0

6 Answers 6

11

If you don't need a constructor it's best to leave it out, no need to write more code. When you DO write it, leave it empty... returning true doesn't have a purpose.

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

Comments

8

There is a difference between the two: If you write an empty __construct() function, you overwrite any inherited __construct() from a parent class.

So if you don't need it and you do not want to overwrite the parent constructor explicitly, don't write it at all.

Comments

5

EDIT:

previous answer is no longer valid, since PHP now behaves like other oop programming languages. constructors aren't part of interfaces. therefore you are now allowed to override them how you prefer without any issues whatsoever

the only exception to this is:

interface iTest
{
    function __construct(A $a, B $b, Array $c);
}

class Test implements iTest
{
    function __construct(A $a, B $b, Array $c){}
    // in this case the constructor must be compatible with the one specified in the interface
    // this is something that php allows but that should never be used
    // in fact as i stated earlier, constructors must not be part of interfaces
}

PREVIOUS OLD NOT-VALID-ANYMORE ANSWER:

there is an important difference between an empty constructor and no constructor at all

class A{}

class B extends A{
     function __construct(ArrayObject $a, DOMDocument $b){}
}

VS

class A{
     function __construct(){}
}
class B extends A{
    function __construct(ArrayObject $a, DOMDocument $b){}
}

// error B::__construct should be compatible with A constructor

1 Comment

Not only that, but if A had a defined constructor and B had a defined, empty, constructor, then you'd essentially be removing the constructor, but if you leave it out altogether then you're inheriting the parent constructor. The upshot is that you shouldn't "always" or "never" include an empty constructor, and it doesn't "always" mean the same thing when you do one or the other. It's all about context.
3

You should only define an empty constructor if your object should never be instantiated. If that is the case, make the __construct() private.

Comments

1

constructor always return instance of class in which its defined . Hence you never use "return" inside constructor . Lastly its better not to define it if you are not gona use it .

2 Comments

The return value of the constructor is completely ignored.
indeed if I find someone returning inside constructor in first place I am never going to merge his code with mine.
0

One reason you might want to define an empty constructor is when you want to avoid calling a function that has the same class name.

class FooBar {
    function foobar() {
        echo "Hello world";
    }
}
new FooBar(); // outputs "Hello world" in  PHP < 8

This is due PHP 4 backwards compatibility, where constructors had the same name of the class. Anyway it got deprecated in PHP 7.4.26.

class FooBar {
    function __construct() {
    }
    function foobar() {
        echo "Hello world";
    }
}
new FooBar(); // no output

Comments

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.