php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #26921 Redefined property in child class modified out of context in parent class
Submitted: 2004-01-15 10:54 UTC Modified: 2004-01-16 09:25 UTC
From: jamie at sailor dot org dot uk Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 4.3.4 OS: Win2K
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: jamie at sailor dot org dot uk
New email:
PHP Version: OS:

 

 [2004-01-15 10:54 UTC] 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'


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-01-16 09:25 UTC] sniper@php.net
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)

 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jul 02 04:01:38 2025 UTC