php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #26841 Child class cannot see protected member var from parent
Submitted: 2004-01-08 07:08 UTC Modified: 2004-07-22 14:53 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: andrey@php.net Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 5CVS-2004-03-15 OS: *
Private report: No CVE-ID: None
 [2004-01-08 07:08 UTC] andrey@php.net
Description:
------------
Hello,..
today I tried the code which I post as reproduce code. I expect in public_func2() the property which is declared as protected in the base class to be visible. However PHP bails out with message :
PHP Fatal error:  Cannot access protected property base::$a in /home/andrey/test/t.php on line 16 . Use of parent:: does not help also.
AFAIK protected member vars should be visible in the generalization classes.

As a side note please refer to bug #18024, for an additional effect which can be seen when using the reproduce code - shadowing of the variable declared in the base class. Method public_func1() of the base class uses the variable $a of "child" which is shadowing $a of "base". Is this an expected behaviour?

Thanks


Reproduce code:
---------------
<?php
class base {
        protected $a = array();

        public function public_func1() {
                var_dump($this->a);
        }
}

class child extends base {
        public $a = 2;

        public function public_func2() {
                var_dump($this->a);
                var_dump(parent::$a);
        }
}

$a = new child();
$a->public_func1();
$a->public_func2();
?>

Expected result:
----------------
int(2)
int(2)
array(0) {
}


Actual result:
--------------
int(2)
int(2)
PHP Fatal error:  Cannot access protected property base::$a in /home/andrey/test/t.php on line 16


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-01-12 07:19 UTC] andrey@php.net
Sorry,
the actual output is :

int(2)
int(2)
PHP Fatal error:  Cannot access protected property base::$a in /home/andrey/test/t.php on line 15
 [2004-06-04 06:35 UTC] nlhowell at cableone dot net
While this *is* wrong (imo), I also see another problem, which is that there's not enough qualification with this.

IMO, 'parent::$a' should refer to the static $a, while '$this->parent::$a' should refer to the derived $a, much like 'self::$a' refers to the static $a and '$this->a' refers to the instance $a. Although you can't have a static $a and a public $a, you can have a static $a that operates as both.

Somehow this should be changed to provide a consistant way to access static and instance members.

Nick
 [2004-07-22 14:53 UTC] stas@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.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Oct 09 03:01:28 2024 UTC