Re: Overloading Constructors

From: Date: Mon, 25 Nov 2002 12:26:35 +0000
Subject: Re: Overloading Constructors
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-125920@lists.php.net to get a copy of this message
At 11:57 25.11.2002, Dan Field said: --------------------[snip]-------------------- >is it doable? > >class myClass { > var $myInt; > > // default constructor > function myClass() { > } > > // overloaded constructor > function myClass($newInt) { > $this->myInt = $newInt; > } >} --------------------[snip]-------------------- At least not with PHP 4.2: <?php class A { function A() { echo "default constructor A()\n"; } function A($string) { echo "overloaded constructor A($string)\n"; } } $a1 = new A(); $a2 = new A('Test'); ?> Results in: overloaded constructor A() overloaded constructor A(Test) thus bypassing the default constructor and calling the overloaded constructor both times. What you might do to distinguish which "overload" has been called: function A($string=null) { if (!isset($string)) echo "Default constructor A()\n"; else echo "Overloaded constructor A($string)\n"; } Hope this helps, -- >O Ernest E. Vogelsinger (\) ICQ #13394035 ^ http://www.vogelsinger.at/

« previous php.general (#125920) next »