From:             php at dayclan dot org
Operating system: Windows Server 2003
PHP version:      5.1.4
PHP Bug Type:     Class/Object related
Bug description:  Inheritance Problem: Private members can't be overridden by 
child classes

Description:
------------
If a parent and child class both define the same member variable, and if
the visibility of the member variable in the parent is "private", all
references in the parent class will access that private variable.  If the
parent variable is protected or public, though, all references in the
parent class will access the child's variable.

The reason I suspect this is a bug and not designed behavior is because
the same is not true for member functions.  If a parent class has a
private member function and the child class defines the same member
function with protected visibility, all references to the function in the
parent class will go to the child class.

It's also annoying if you're using a child class to override a subset of
the functionality in a parent class. 

Reproduce code:
---------------
class Class1
{   
   private $var1 = 'Class1 var1';
   protected $var2 = 'Class1 var2';
   
   public function printOut() {
      echo $this->var1 . "\n";
      echo $this->var2 . "\n";
      echo $this->func1();
   }

   private function func1() {
      echo 'Class1 func1';
   }
}

class Class2 extends Class1
{
   protected $var1 = 'Class2 var1';
   protected $var2 = 'Class2 var2';

   protected function func1() {
      echo 'Class2 func1';
   }
}

$class2 = new Class2();
$class2->printOut();

Expected result:
----------------
Class2 var1
Class2 var2
Class2 func1

Actual result:
--------------
Class1 var1
Class2 var2
Class2 func1

-- 
Edit bug report at http://bugs.php.net/?id=37320&edit=1
-- 
Try a CVS snapshot (PHP 4.4): 
http://bugs.php.net/fix.php?id=37320&r=trysnapshot44
Try a CVS snapshot (PHP 5.1): 
http://bugs.php.net/fix.php?id=37320&r=trysnapshot51
Try a CVS snapshot (PHP 6.0): 
http://bugs.php.net/fix.php?id=37320&r=trysnapshot60
Fixed in CVS:                 http://bugs.php.net/fix.php?id=37320&r=fixedcvs
Fixed in release:             
http://bugs.php.net/fix.php?id=37320&r=alreadyfixed
Need backtrace:               http://bugs.php.net/fix.php?id=37320&r=needtrace
Need Reproduce Script:        http://bugs.php.net/fix.php?id=37320&r=needscript
Try newer version:            http://bugs.php.net/fix.php?id=37320&r=oldversion
Not developer issue:          http://bugs.php.net/fix.php?id=37320&r=support
Expected behavior:            http://bugs.php.net/fix.php?id=37320&r=notwrong
Not enough info:              
http://bugs.php.net/fix.php?id=37320&r=notenoughinfo
Submitted twice:              
http://bugs.php.net/fix.php?id=37320&r=submittedtwice
register_globals:             http://bugs.php.net/fix.php?id=37320&r=globals
PHP 3 support discontinued:   http://bugs.php.net/fix.php?id=37320&r=php3
Daylight Savings:             http://bugs.php.net/fix.php?id=37320&r=dst
IIS Stability:                http://bugs.php.net/fix.php?id=37320&r=isapi
Install GNU Sed:              http://bugs.php.net/fix.php?id=37320&r=gnused
Floating point limitations:   http://bugs.php.net/fix.php?id=37320&r=float
No Zend Extensions:           http://bugs.php.net/fix.php?id=37320&r=nozend
MySQL Configuration Error:    http://bugs.php.net/fix.php?id=37320&r=mysqlcfg

Reply via email to