note 15636 added to language.oop.constructor

From: Date: Mon, 24 Sep 2001 12:04:43 +0000
Subject: note 15636 added to language.oop.constructor
Groups: php.notes 
Request: Send a blank email to php-notes+get-18246@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- -- http://www.php.net/manual/en/language.oop.constructor.php http://master.php.net/manage/user-notes.php?action=edit+15636 http://master.php.net/manage/user-notes.php?action=delete+15636 http://master.php.net/manage/user-notes.php?action=reject+15636

« previous php.notes (#18246) next »