ID: 37212
Comment by: crescentfreshpot at yahoo dot com
Reported By: andreasblixt at msn dot com
Status: Open
Bug Type: Class/Object related
Operating System: Irrelevant
PHP Version: 5.1.2
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2006-04-26 18:37:27] andreasblixt at msn dot com
While looking for a workaround I found that the result of the test on a
protected method instead of a protected property is different
(successful):
Workaround:
-----------
Add a protected method in the same class in which the protected
property is defined that returns the value of the property. Then call
the method in place of attempting to access the property.
------------------------------------------------------------------------
[2006-04-26 18:13:51] andreasblixt at msn dot com
In addition to the above description I would like to add that the
sub-classes CAN access each other's protected variables, if the
accessing is done in a shared parent class.
------------------------------------------------------------------------
[2006-04-26 18:10:02] 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
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=37212&edit=1