Coder Perfect

In PHP, why can’t I overload constructors?

Problem

I’ve given up all chance of ever being able to overload my constructors in PHP, therefore I’m curious as to why.

Is there any justification for it? Does it produce code that is fundamentally bad? Is it a widely acknowledged language design not to enable it, or are there alternative languages that are more pleasant to use than PHP?

Asked by tom

Solution #1

In PHP, you can’t overload ANY method. Use the factory pattern with a private constructor if you want to be able to instantiate a PHP object while passing multiple alternative combinations of parameters.

For example:

public MyClass {
    private function __construct() {
    ...
    }

    public static function makeNewWithParameterA($paramA) {
        $obj = new MyClass(); 
        // other initialization
        return $obj;
    }

    public static function makeNewWithParametersBandC($paramB, $paramC) {
        $obj = new MyClass(); 
        // other initialization
        return $obj;
    }
}

$myObject = MyClass::makeNewWithParameterA("foo");
$anotherObject = MyClass::makeNewWithParametersBandC("bar", 3);

Answered by Alex Weinstein

Solution #2

To achieve the same result, use variable parameters. Given default arguments and all of the other “workarounds,” it doesn’t make much sense to add without strong typing.

Answered by pestilence669

Solution #3

I’ll throw in Fluent Interfaces for good measure. The concept is that you can chain calls together by adding return $this; to the end of your methods. As an alternative to

$car1 = new Car('blue', 'RWD');
$car2 = new Car('Ford', '300hp');

(which would just not work), you can do the following:

$car = (new Car)
       ->setColor('blue')
       ->setMake('Ford')
       ->setDrive('FWD');

You’ll be able to pick and choose which properties you want to change. It’s similar to giving an array of options to your initial call in many ways:

$car = new Car(['make' => 'Ford', 'seats' => 5]);

Answered by Codemonkey

Solution #4

True overloading is not possible in PHP. You can use variable parameters, like @Pestilence mentioned. Some people just use an Associative Array of various options to overcome this.

Answered by Dominic Barnes

Solution #5

Function Arguments and Default Values in PHP

I just used default values for function parameters to get around this. List the required parameters first in __constuct. After that, list the optional parameters in the general form $param = null.

class User
{
    private $db;
    private $userInput;

    public function __construct(Database $db, array $userInput = null)
    {
        $this->db = $db;
        $this->userInput = $userInput;
    }
}

This can be expressed as:

$user = new User($db)

or

$user = new User($db, $inputArray);

This isn’t a perfect solution, but I was able to make it work by splitting parameters into absolute mandatory parameters that must be satisfied regardless of when the object is built, and optional parameters listed in order of importance as a group.

It works.

Answered by Anthony Rutledge

Post is based on https://stackoverflow.com/questions/2169448/why-cant-i-overload-constructors-in-php