This is a compiled example, it becomes much more useful when there are many parameters.
This will allow the subscriber to use either new Person("Jim", 1950, 10, 2) or new Person("Jim", datetimeobj) . I know about advanced options, this is not what I am looking for here.
In C #, I can do:
public Person(string name, int birthyear, int birthmonth, int birthday) :this(name, new DateTime(birthyear, birthmonth, birthday)){ } public Person(string name, DateTime birthdate) { this.name = name; this.birthdate = birthdate; }
Is it possible to do something like this in PHP? Sort of:
function __construct($name, $birthyear, $birthmonth, $birthday) { $date = new DateTime("{$birthyear}\\{$birthmonth}\\{$birthyear}"); __construct($name, $date); } function __construct($name, $birthdate) { $this->name = $name; $this->birthdate = $birthdate; }
If this is not possible, what is a good alternative?
source share