Andrew, Thanks for your reply. There still is something I don't understand.
I understand that my derived class inherits all the member variables and functions of the base class. I would expect however that upon redeclaring a variable in a derived class, the variable in the derived class "shadows" the variable in the base class (as it does in java). The PHP behaviour mimics for variables the behaviour for member functions. This behaviour on functions is necessary to have polymorphism, but I believe it is dangerous for variables. Imagine I define a base class of shapes, in which I declare a variable called $depth representing the size of my object on the z axis. Assume someone (could be me) derives a colored_shape class from my shape class, and declares a variable called $depth to represent the color depth. Evidently, setting the $depth variable in the class colored_shape will change the shape of the object, which is probably not the desired behaviour. The current treatment of member variables in PHP forces a programmer deriving from a class to know the names of all the variables defined in the base class and its (recursive) parents in order to avoid corrupting them. It seems to me that this hinders the reuse of classes, which I believe is one of the main objectives of OOP. Regards, Alain "Andrew Kirilenko" <[EMAIL PROTECTED]> a écrit dans le message de news: [EMAIL PROTECTED] > Hello! > > > Is there something I don't understand ? > Yes. > > According OOP concepts, derived class inherits all functionality and data > from the base. So, if you have $shared variable in the base class, your > derived class will have it automatically. But PHP allows you to declare this > variable once again in the derived class. In C++ you will get comilation > error in this case. > > Best regards, > Andrew Kirilenko. > > > -----Original Message----- > > From: Alain Dresse [mailto:[EMAIL PROTECTED]] > > Sent: Saturday, October 27, 2001 10:12 PM > > To: [EMAIL PROTECTED] > > Subject: [PHP] overloading variables in child classes > > > > > > Hi all, > > > > I am a bit puzzled by the way PHP treates variables that have the > > same name > > in a base class and a child class. > > It seems to me that if a child class and its parent both have a variable > > with the same name, then they should be different variables. Instead, the > > example below indicates that they is only one variable : > > ---- > > class base { > > var $shared; > > function base() {$this->shared = "base";} > > function test() {echo("\$shared in base : " . $this->shared . > > "<br>\n");} > > } > > > > class child extends base { > > var $shared; > > function child(){$this->base(); $this->shared = "child";} > > function test() { > > echo ("\$shared in child : " . $this->shared . "<br>\n"); > > parent::test(); > > } > > } > > > > $the_child = new child(); > > $the_child->test(); > > ---- > > > > I would have expected the output to be > > $shared in child : child > > $shared in base : base > > > > instead I have > > $shared in child : child > > $shared in base : child > > > > Is there something I don't understand ? > > > > Regards, > > Alain Dresse > > [EMAIL PROTECTED] > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]