php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #37212 Access to protected property of common base class
Submitted: 2006-04-26 18:10 UTC Modified: 2006-05-27 19:58 UTC
From: andreasblixt at msn dot com Assigned: helly (profile)
Status: Closed Package: Class/Object related
PHP Version: 5.1.* OS: *
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: andreasblixt at msn dot com
New email:
PHP Version: OS:

 

 [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


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-04-26 18:13 UTC] 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:37 UTC] 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-27 15:02 UTC] 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-27 15:41 UTC] 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 19:39 UTC] 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-05-05 19:03 UTC] mike@php.net
Probably a missing check if the property was declared in a common parent class?
 [2006-05-27 01:40 UTC] helly@php.net
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-27 02:05 UTC] helly@php.net
Fix requies API change so it is not doable in 5.1.
 [2006-05-27 02:14 UTC] helly@php.net
Fixed in HEAD so far.
 [2006-05-27 19:58 UTC] helly@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.

Fixed in 5.2, cannot fix in 5.0/5.1
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 16:01:28 2024 UTC