Re: Bug #13842 Updated: Member variables in parent and child classes overwrite each other

From: Date: Sun, 28 Oct 2001 16:36:57 +0000
Subject: Re: Bug #13842 Updated: Member variables in parent and child classes overwrite each other
References: 1  Groups: php.dev 
Request: Send a blank email to php-dev+get-69189@lists.php.net to get a copy of this message
Pleaes use the web interface for updating bugs! I missed this mail until now. --Jeroen ----- Original Message ----- From: "Doug Plant" <dfplant@hotmail.com> Newsgroups: php.dev To: <php-dev@lists.php.net> Sent: Sunday, October 28, 2001 4:38 AM Subject: Re: Bug #13842 Updated: Member variables in parent and child classes overwrite each other > > Hi, > > Thanks for the reply. Still looks bad. > > My mistake, but I left the script in it's "good" configuration. If you > change the line: > > class C2 extends C1_1 > > To: > > class C2 extends C1 > > the the bad behaviour is shown. I left the line in the former state. > > Doug > > > > >From: Bug Database <php-dev@lists.php.net> > >To: dfplant@hotmail.com > >Subject: Bug #13842 Updated: Member variables in parent and child classes > >overwrite each other > >Date: 27 Oct 2001 05:37:46 -0000 > > > >ID: 13842 > >Updated by: sniper > >Reported By: dfplant@hotmail.com > >Old Status: Open > >Status: Feedback > >Bug Type: Class/Object related > >Operating System: Windows NT 5.0 build 2195 > >PHP Version: 4.0.6 > >New Comment: > > > >Works fine for me with latest CVS. Please try the > >development build from http://www.php4win.com/ > > > >--Jani > > > > > > > >Previous Comments: > >------------------------------------------------------------------------ > > > >[2001-10-26 17:52:30] dfplant@hotmail.com > > > >I'm not all that familiar with PHP, so it's possible I haven't understood > >something that should be obvious, or have mistyped something. But assuming > >that's not the case: > > > >To my eye this makes using someone else's classes via inheritence a wee bit > >dangerous, and makes using inheritence at all somewhat problematic since > >changing a class that exists in an inheritence makes one liable to create > >subtle bugs in code that one has not modified - which defeats one of the > >(possibly the most) principal purposes of OO. > > > >If I could make a suggestion: support of 'private' and 'public' for member > >variables might be a good thing. Especially if access defaulted to > >'private' and that meant that the variable was only visible to the > >immediate class. > > > >Using the CGI binary without modification running on "Microsoft-IIS/5.0" I > >used the following test script: > > > ><html> > ><head> > ><title>Inheritence Bug</title> > ></head> > ><body> > > > ><?php > > file://===================================================================== ==== > > // NOTE: > > // Bad behaviour if C2 extends C1, Correct if C2 extends C1_1 > > class C2 extends C1_1 > > { > > var $m_sMe = "C2"; // ERROR: this overwrites parent > > var $m_sIniter = ""; > > > > function C2( $sIniter="") > > { > > $this->m_sIniter = $sIniter; // OK - by defn > > parent::C1( $this->m_sMe); // OK - the way to do it > > } > > > > function WhoIsIt() > > { > > printf( "C2::WhoIsIt() : This is: " . $this->m_sMe . > > "<br>\n"); > > printf( "Inited by: " . $this->m_sIniter . "<br>\n"); > > > > parent::WhoIsIt(); > > } > > } > > > > file://===================================================================== ==== > > class C1_1 > > { > > var $m_sMe_1 = "C1"; > > var $m_sIniter_1 = ""; > > > > function C1( $sIniter="") > > { > > print( "(OK)<br>\n"); > > $this->m_sIniter_1 = $sIniter; > > } > > > > function WhoIsIt() > > { > > printf( "C1::WhoIsIt() : This is: " . $this->m_sMe_1 . > > "<br>\n"); > > printf( "Inited by: " . $this->m_sIniter_1 . "<br>\n"); > > } > > > > function ResetBase() > > { > > $this->m_sMe_1 = "C1"; > > > > printf( "C1::ResetBase() : This is: " . $this->m_sMe_1 . > > "<br>\n"); > > printf( "Inited by: " . $this->m_sIniter_1 . "<br>\n"); > > } > > } > > > > file://===================================================================== ==== > > class C1 > > { > > var $m_sMe = "C1"; > > var $m_sIniter = ""; > > > > function C1( $sIniter="") > > { > > print( "(Bad)<br>\n"); > > $this->m_sIniter = $sIniter; // ERROR: this overwrites child's value > > } > > > > function WhoIsIt() > > { > > printf( "C1::WhoIsIt() : This is: " . $this->m_sMe . > > "<br>\n"); > > printf( "Inited by: " . $this->m_sIniter . "<br>\n"); > > } > > > > function ResetBase() > > { > > $this->m_sMe = "C1"; > > > > printf( "C1::ResetBase() : This is: " . $this->m_sMe . > > "<br>\n"); > > printf( "Inited by: " . $this->m_sIniter . "<br>\n"); > > } > > } > > > > file://===================================================================== ==== > > $test = new C2( "Doug"); > > $test->WhoIsIt(); > > file://$test->ResetBase(); > >?> > > > ><p> > >In case there is a platform dependency, this is what I consider correct > >output:<br> > ><pre> > >C2::WhoIsIt() : This is: C2 > >Inited by: Doug > >C1::WhoIsIt() : This is: C1 > >Inited by: C2 > ></pre> > ></p> > > > ></body> > ></html> > > > > > >------------------------------------------------------------------------ > > > > > > > >ATTENTION! Do NOT reply to this email! > >To reply, use the web interface found at > >http://bugs.php.net/?id=13842&edit=2 > > > > > _________________________________________________________________ > Get your FREE download of MSN Explorer at > http://explorer.msn.com/intl.asp >

« previous php.dev (#69189) next »