|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2006-04-26 18:10 UTC] andreasblixt at msn dot com
Description: ------------ In class structures where the sub-classes are extended non-linearly (i.e. multiple classes on the same tier), the sub-classes in different lines cannot access variables defined as protected in a shared parent class in eachother. Reproduce code: --------------- http://pastebin.com/683438 Expected result: ---------------- Tier3A accessing Tier3C... OK! ('Value from Tier3C') Tier3B accessing Tier3C... OK! ('Value from Tier3C') Actual result: -------------- Tier3A accessing Tier3C... OK! ('Value from Tier3C') Tier3B accessing Tier3C... Fatal error: Cannot access protected property Tier3C::$value in protected.php5 on line 31 PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Oct 25 15:00:01 2025 UTC |
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.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.