note 15636 deleted from language.oop.constructor by jimw
| From: | jimw@php.net | Date: | Sat, 08 Dec 2001 23:44:30 +0000 |
| Subject: | note 15636 deleted from language.oop.constructor by jimw | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-22370@lists.php.net to get a copy of this message | ||
About multiple constructors.<br>
So far PHP has no support for multiple<br> constructors either than default values,<br>
but this can be 'simulated' in the following manner:<br>
class thing {
/* this is our argument list */
var $arglist;
function thing_integer() {
/* constructor thing(int)
arglist[0] now contains an int
*/
}
function thing_string_string() {
/* contructor thing(string)
arglist[0]&arglist[1] now
contains strings
*/
}
function thing() {
/* get number of arguments */
$num_args=func_num_args();
/* go through the argument list */
if($num_args) {
/* get the argument list */
$this->arglist=func_get_args();
/* get classname part of func */
$ctor=get_class($this);
for($i=0;$i<num_args;$i++) {
$ctor.="_".gettype($this->arglist[$i]);
}
/* call new constructor */
call_user_method($ctor,$this);
} else {
/* no arguments, default ctor */
}
}
}
<br>
Basically, what happens is that you build up a string<br> containing the base classname,
appended with an _argument-type.<br> In this way you may have several
constructors.<br><br>
-anders-