1

In Java, I enjoy the flexibility of having 1 to x number of constructors depending my need and the number of properties/attributes my class have.

class Foo{
    private int id;
    private boolean test;
    private String name;

    public Foo(){
    }

    public Foo(int id){
        this.id=id;
    }

    public Foo(boolean test){
        this.test=test;
    }

    public Foo(int id, boolean test){
        this.id=id;
        this.test=test;
    }
}

Unlike in PHP I can only have one constructor from what I have learnt so far.

class Foo{
    private $id;
    private $test;
    private $name;

    function __construct() {
    }
}

Or

class Foo{
    private $id;
    private $test;
    private $name;

    function __construct($id, $test, $name) {
        $this->id=$id;
        $this->test=$test;
        $this->name=$name;
    }
}

Or any other combinations;

What I do: Most time I prefer populating these properties using the getters and setters but this can result in writing a lot of codes for classes with some number of properties. I think there could be some better approaches:

My questions are two:

  1. Why can't I overload PHP constructors? I want to know reason behind this limitation
  2. What is the best why of populating a PHP object properties?
Paullo
  • 2,038
  • 4
  • 25
  • 50
  • 1
    php has no explicits types. There's little point in constructor overloading if you can't differentiate between `__construct($a,$b)` and `__construct($a,$b)` (If those look the same, that's because that's what PHP would think, too!) – Mike 'Pomax' Kamermans Aug 20 '14 at 05:55
  • You could always look at the magic __get() and __set() methods if you do not want to keep doing getters and setters. – Michal Aug 20 '14 at 05:57
  • I presume __get() and __set() are inbuilt functions. Though I don't have any idea about their behaviors but i will check it out – Paullo Aug 20 '14 at 06:01
  • 1
    Rather than overloading methods, php uses default parameters, which can accomplish the same things as an overloaded java constructor. Please read the manual and check out the section on default parameters. http://php.net/manual/en/functions.arguments.php Also, calling `func_get_args()` will return an array of all arguments, similar to an `argv` constructor. – chiliNUT Aug 20 '14 at 06:03
  • And for multiple signatures that accept the same number of arguments, you can check for the type of variable passed (int, boolean, etc) and then branch accordingly. – chiliNUT Aug 20 '14 at 06:05

1 Answers1

3

Two things:

  • You can use func_get_args() to retrieve and inspect the passed arguments. Note that PHP doesn't even check the number of arguments, so it can be function foo() while inside many arguments are handled.
  • Use a so-called fluent interface, i.e. a chain of setters that each return $this. This then becomes Foo::create()->setId(42)->setName('blah'), which is almost as readable as Pythons named parameters.
Ulrich Eckhardt
  • 16,572
  • 3
  • 28
  • 55
  • Your second point looks so interesting! I never knew something like that exists. I will check it out – Paullo Aug 20 '14 at 06:06