note 104070 added to language.oop5.decon
| From: | 3dboxdotplatgmaildotcom at osu1 dot php dot net | Date: | Fri, 20 May 2011 20:58:43 +0000 |
| Subject: | note 104070 added to language.oop5.decon | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-179916@lists.php.net to get a copy of this message | ||
@phenixdoc at gmail dot com
Your way and inheriting, makes multiple constructors almost language feature ;)
<?php
header('Content-type: text/plain');
abstract class MultipleConstructor {
public function __construct() {
$args = func_get_args();
if (count($args) == 0) {
if (method_exists($this, '__construct_void')) {
$this->__construct_void();
return;
}
}
$constructName = '__construct';
foreach ($args as $arg) {
$constructName .= '_' . gettype($arg);
}
if (method_exists($this, $constructName)) {
call_user_method_array($constructName, $this, $args);
return;
} else {
throw new Exception('Constructor: ' . $constructName . ' not
exist!');
}
}
}
/* ----------------------------------------------------- */
class User extends MultipleConstructor {
private $name;
private $age;
public function __construct_void() {
$this->name = 'Mat';
$this->age = 24;
}
protected function __construct_string($name) {
$this->name = $name;
$this->age = 48;
}
protected function __construct_string_integer($name, $age) {
$this->name = $name;
$this->age = $age;
}
}
$mat = new User();
print_r($mat);
$john = new User('John');
print_r($john);
$andrew = new User('Andrew', 96);
print_r($andrew);
?>
-----------------------------------------------
User Object
(
[name:User:private] => Mat
[age:User:private] => 24
)
User Object
(
[name:User:private] => John
[age:User:private] => 48
)
User Object
(
[name:User:private] => Andrew
[age:User:private] => 96
)
----
Server IP: 195.114.0.19
Probable Submitter: 82.143.147.29
----
Manual Page -- http://www.php.net/manual/en/language.oop5.decon.php
Edit -- https://master.php.net/note/edit/104070
Del: integrated -- https://master.php.net/note/delete/104070/integrated
Del: useless -- https://master.php.net/note/delete/104070/useless
Del: bad code -- https://master.php.net/note/delete/104070/bad+code
Del: spam -- https://master.php.net/note/delete/104070/spam
Del: non-english -- https://master.php.net/note/delete/104070/non-english
Del: in docs -- https://master.php.net/note/delete/104070/in+docs
Del: other reasons-- https://master.php.net/note/delete/104070
Reject -- https://master.php.net/note/reject/104070
Search -- https://master.php.net/manage/user-notes.php