#26921 [Opn->Bgs]: Redefined property in child class modified out of context in parent class

From: Date: Fri, 16 Jan 2004 14:25:27 +0000
Subject: #26921 [Opn->Bgs]: Redefined property in child class modified out of context in parent class
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-53434@lists.php.net to get a copy of this message
ID: 26921 Updated by: sniper@php.net Reported By: jamie at sailor dot org dot uk -Status: Open +Status: Bogus Bug Type: Class/Object related Operating System: Win2K PHP Version: 4.3.4 New Comment: You call the parent's constructor in the child, of course it sets the variable again. (this is 'fixed' in PHP 5 with the introduction of private/protected/public variables) Previous Comments: ------------------------------------------------------------------------ [2004-01-15 10:54:54] jamie at sailor dot org dot uk Description: ------------ I define variable A within parent class cFoo, and initialise it within the class Constructor. I redefine variable A within child class cBar, and initialise it within the class Constructor which then calls the parent class Constructor. The parent constructor modifies the child instance of property A rather than the parent instance. Reproduce code: --------------- <?php /* Inheritance test2. Inherited Vars */ class cFoo { var $A; function cFoo() /* constructor */ { $this->A = "cFoo_A"; } function Display() { print("Within cFoo::Display; property A = '$this->A'; should be 'cFoo_A'<br>\n"); } }; class cBar extends cFoo { var $A; // redefines A function cBar() /* constructor */ { $this->A = "cBar_A"; $this->cFoo(); /* parent constructor */ } function Display() { print("Within cBar::Display; property A = '$this->A'; should be 'cBar_A'<br>\n"); } }; $foo1 = new cFoo; $foo1->Display(); $bar1 = new cBar; $bar1->Display(); exit; ?> Expected result: ---------------- Within cFoo::Display; property A = 'cFoo_A'; should be 'cFoo_A' Within cBar::Display; property A = 'cBar_A'; should be 'cBar_A' Actual result: -------------- Within cFoo::Display; property A = 'cFoo_A'; should be 'cFoo_A' Within cBar::Display; property A = 'cFoo_A'; should be 'cBar_A' ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=26921&edit=1

« previous php.bugs (#53434) next »