ID: 37212 Updated by: [EMAIL PROTECTED] -Summary: Protected variables not always accessible between sub-classes Reported By: andreasblixt at msn dot com Status: Assigned Bug Type: Class/Object related Operating System: * -PHP Version: 5.1.4 +PHP Version: 5.1.* Assigned To: helly New Comment:
Fix requies API change so it is not doable in 5.1. Previous Comments: ------------------------------------------------------------------------ [2006-05-27 01:40:49] [EMAIL PROTECTED] The problem is that right now struct property_info does not know where the property was declared. Thus what the engine sees is B accessing some protected property in C. The fix seems however quite easy. I'll give it a try. ------------------------------------------------------------------------ [2006-05-05 19:03:33] [EMAIL PROTECTED] Probably a missing check if the property was declared in a common parent class? ------------------------------------------------------------------------ [2006-04-27 19:39:38] crescentfreshpot at yahoo dot com Odd. Here is a complete example. <?php class A { protected $value; public function __construct($val) { $this->value = $val; } public function copyValue($obj) { $this->value = $obj->value; } protected function getValue() { return $this->value; } } class B extends A { public function copyValue($obj) { $this->value = $obj->getValue(); // this works $this->value = $obj->value; // this is fatal } } class C extends A {} $B = new B("Value from B"); $C = new C("Value from C"); $B->copyValue($C); ?> Strange that the method call works but the property access doesn't. Both are protected. ------------------------------------------------------------------------ [2006-04-27 15:41:26] andreasblixt at msn dot com The property is not being redeclared in C, though. It is still a property of A, structure-wise. A method declared and called in the same way as the property does not cause any error. Here's a simple non-code example: protected method() is declared in A. protected $property is declared in A. B and C both extend A. Method in B tries to access $C->property [FATAL ERROR] Method in B tries to access $C->method() [No error] One could even make method() return $this->property which would not cause an error either. It would be expected that both the property and method would have the same visibility when declared in the same class. ------------------------------------------------------------------------ [2006-04-27 15:02:13] crescentfreshpot at yahoo dot com Here is simplified reproduce code: <?php class A { protected $value; public function __construct($val) { $this->value = $val; } public function copyValue($obj) { $this->value = $obj->value; } } class B extends A { public function copyValue($obj) { $this->value = $obj->value; } } class C extends A {} $B = new B("Value from B"); $C = new C("Value from C"); $B->copyValue($C); // fatal var_dump($B); ?> I'm not sure that this is a bug. Class B's copyValue() is trying to access a protected member of Class C, which is not in B's chain of inheritance. ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at http://bugs.php.net/37212 -- Edit this bug report at http://bugs.php.net/?id=37212&edit=1