ID:               37212
 Updated by:       [EMAIL PROTECTED]
 Reported By:      andreasblixt at msn dot com
-Status:           Open
+Status:           Assigned
 Bug Type:         Class/Object related
 Operating System: Irrelevant
 PHP Version:      5.1.2
-Assigned To:      
+Assigned To:      helly
 New Comment:

Probably a missing check if the property was declared in a common
parent class?


Previous Comments:
------------------------------------------------------------------------

[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.

------------------------------------------------------------------------

[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.

------------------------------------------------------------------------

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

Reply via email to