1

I have a question. I have some experience with OOP, but I am new to PHP(OOP). I am trying to learn it. Is it possible to overload a constructor?

In other languages it's possible to overload the constructor but in php not? I tried but my I got the following message from my IDE: "method with same name already defined in this class"

class testClass{

    public $param1 = "";
    public $param2 = "";

    public function __construct($param1, $param2){        
        $this->param1 = $param1;
        $this->param2 = $param2;

    }

    public function __construct($param1){
        $this->param1 = $param1;
        //ToDO use function to set param2
    }
}
Richard A Quadling
  • 3,769
  • 30
  • 40
EJW
  • 338
  • 3
  • 6
  • 18

3 Answers3

5

The best way to do this is with an optional param, as others have said in the comments, PHP does not support overloading, so this would be my suggestion:

class Foo
{
    public function __construct($p1, $p2 = null)
    {
        $this->p1 = $p1;
        $this->p2 = $p2;
    }
}

This means that you can choose to use $p2 when creating the class but you must use $p1.

AFAIK, this is the only form of overloading (if you can call it that) that exists as yet in PHP

Edit

An extension from kingkero's answer, I came up with this solution which keeps logic out of the constructor minus a function call:

<?php
class overload
{
    public function __construct() 
    {
        return $this->switchConstruct(func_get_args());
    }

    protected function switchConstruct(array $args)
    {
        switch (count($args))
        {
            case 0:
                return print "0 params<br />";
            case 1:
                return call_user_func_array(array($this, 'constr1'), $args);
            case 2:
                return call_user_func_array(array($this, 'constr2'), $args);
        }
        die("Invalid number of args");
    }

    protected function constr1($a) 
    {
        print "constr1 called<br />";
    }

    protected function constr2($a, $b) 
    {
        print "constr2 called<br />";
    }
}
Can O' Spam
  • 2,718
  • 4
  • 19
  • 45
3

No you cannot overload methods like that.

The best you can do is use optional parameters:

class testClass{

    public $param1 = "";
    public $param2 = "";

    public function __construct($param1, $param2=null){        
        $this->param1 = $param1;
        if($param2){
            $this->param2 = $param2;
        }else{
            //ToDO use function to set pramam2
        }
    }
}
Steve
  • 20,703
  • 5
  • 41
  • 67
  • As I have been told many times, constructors should not contain login, so the `if` statements you use are not suitable for a constructor – Can O' Spam Dec 21 '15 at 11:40
  • 1
    @SamSwift웃 Well, constructor overloading is usually used for Poor Mans / Bastard injection, which is considered an antipattern anyway, so i dont see how this is any different http://stackoverflow.com/questions/7099406/what-is-the-real-difference-between-bastard-injection-and-poor-mans-injectio – Steve Dec 21 '15 at 11:44
1

As was already stated, you cannot do it like this. However, if you want it to be more variable than the given answers, try using func_get_args() in combination with call_user_func_array() like so (demo)

class Foo {
  public function __construct() {
    $args = func_get_args();
    switch (count($args)) {
      case 2:
        return call_user_func_array(
          array($this, 'constructorFor2Arguments'),
          $args
        );
      //...
    }
  }

  protected function constructorFor2Arguments($one, $two) {
    //...
  }
}
kero
  • 10,647
  • 5
  • 41
  • 51