note 102882 modified in language.oop5.decon by danbrown
| From: | danbrown@php.net | Date: | Sat, 12 Mar 2011 00:51:50 +0000 |
| Subject: | note 102882 modified in language.oop5.decon by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-177812@lists.php.net to get a copy of this message | ||
multiple constructors with type signetures (almost overloading):
<?php
public function __construct() {
$args = func_get_args();
$argsStr = '';
foreach ($args as $arg) {
$argsStr .= '_' . gettype($arg);
}
if (method_exists($this, $constructor = '__construct' . $argsStr))
call_user_func_array(array($this, $constructor), $args);
else
throw new Exception('NO CONSTRUCTOR: ' . get_class() . $constructor, NULL,
NULL);
}
public function __construct_integer($a) {}
public function __construct_string($a) {}
public function __construct_integer_string($a, $b) {}
public function __construct_string_integer($a, $b) {}
?>
--was--
multiple constructors with type signetures (almost overloading):
public function __construct() {
$args = func_get_args();
$argsStr = '';
foreach ($args as $arg) {
$argsStr .= '_' . gettype($arg);
}
if (method_exists($this, $constructor = '__construct' . $argsStr))
call_user_func_array(array($this, $constructor), $args);
else
throw new Exception('NO CONSTRUCTOR: ' . get_class() . $constructor, NULL,
NULL);
}
public function __construct_integer($a) {}
public function __construct_string($a) {}
public function __construct_integer_string($a, $b) {}
public function __construct_string_integer($a, $b) {}
http://php.net/manual/en/language.oop5.decon.php