php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #73862 Accessing protected property and method of sibling class is inconsistent
Submitted: 2017-01-04 19:05 UTC Modified: 2017-01-11 10:25 UTC
From: after89 at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 7.1.0 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: after89 at gmail dot com
New email:
PHP Version: OS:

 

 [2017-01-04 19:05 UTC] after89 at gmail dot com
Description:
------------
Accessing protected property defined in superclass of current context on object that is sibling of current context class (extends one of the base class) is not possible (https://3v4l.org/BODds). Althrough accessing method in the same manner is valid (https://3v4l.org/8DNBa).

Both of this behaviours should be consistent.



Test script:
---------------
<?php

class Base { protected $foo = 'Base'; }
class Other extends Base { protected $foo = 'Other'; }
class Child extends Base { public function bar() { $otherBase = new Other(); echo $otherBase->foo;  } }

$child = new Child();
$child->bar();


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-01-04 19:07 UTC] after89 at gmail dot com
Valid code for method:
<?php

class Base { protected function foo() {} }
class Other extends Base { protected function foo() { echo "OK"; } }
class Child extends Base { public function bar() { $otherBase = new Other(); $otherBase->foo();  } }

$child = new Child();
$child->bar();
 [2017-01-05 01:15 UTC] dave at mudsite dot com
This might be a quirk how method inheritance works, as compared to how property inheritance works.  I think if you redefine a property in a subclass the visibility is re-restricted to that class.  For instance you can see: https://3v4l.org/j3aQg .  If you use the __construct to set the $this->foo, there is no scope issue.  Whereas with methods you're defining a signature that can be implemented in extended classes

So I don't know if this is a bug, per se, or just an oddity.  Someone with better knowledge on the intent on property inheritance could probably answer the discrepancy.
 [2017-01-11 05:01 UTC] krakjoe@php.net
-Status: Open +Status: Not a bug
 [2017-01-11 05:01 UTC] krakjoe@php.net
There are errors or assumptions that are wrong in your code.
 [2017-01-11 10:18 UTC] after89 at gmail dot com
What is wrong in my code?
 [2017-01-11 10:25 UTC] after89 at gmail dot com
In my opinion it is wrong that visibility works different for properties and for methods. If it is correct behaviour, please consider noticing this in PHP documentation.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 27 23:01:30 2024 UTC