note 55524 deleted from language.oop5.decon by levim
| From: | levim@php.net | Date: | Tue, 18 Mar 2014 02:10:14 +0000 |
| Subject: | note 55524 deleted from language.oop5.decon by levim | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-198921@lists.php.net to get a copy of this message | ||
Note Submitter: stanley dot turnteen at gmail dot com
----
IMHO using func_get_args() is superior to constructor polymorphism, because you don't have to
define constructors for every possible way a class can be initialized.
The pattern I use looks like this; all you have to do is pass the parameters in the correct order.
<?php
class Movie
{
public $title;
public $director;
public $stars;
public $year_released;
public function __construct()
{
$args = func_get_args();
foreach(array("title", "director", "stars",
"year_released") as $i)
{
if(empty($args))
{
break;
}
$this->$i = array_shift($args);
}
}
}
?>