Re: OOP Question

From: Date: Wed, 07 Apr 2004 21:22:25 +0000
Subject: Re: OOP Question
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-182715@lists.php.net to get a copy of this message
On Wed, 2004-04-07 at 17:07, Richard Lewis wrote: > What do members think that this code should do: > > class A > { > var $a, $b; > function A($a) > { > $this->$a = $a; > } > function prnt() > { > echo "<br>a=" . $this->$a; > } > } > > class B extends A > { > function B($a, $b) > { > parent::A($a); > $this->$b = $b; > } > function prnt() > { > parent::prnt(); > echo "<br>b=" . $this->$b; > } > } > > $obj = new B("a","b"); > $obj->prnt(); > > I think it should print > a=a > b=b > > but it seems to print (on PHP4) > a= > b= Looks like you're suffering from mad dollar disease. You should have the following instead: class A { var $a, $b; function A($a) { $this->a = $a; } function prnt() { echo "<br>a=" . $this->a; } } class B extends A { function B($a, $b) { parent::A($a); $this->b = $b; } function prnt() { parent::prnt(); echo "<br>b=" . $this->b; } } $obj = new B("a","b"); $obj->prnt(); -- .------------------------------------------------------------. | InterJinn Application Framework - http://www.interjinn.com | :------------------------------------------------------------: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `------------------------------------------------------------'

« previous php.general (#182715) next »