Bug #73862 [Nab]: Accessing protected property and method of sibling class is inconsistent
| From: | after89 at gmail dot com | Date: | Wed, 11 Jan 2017 10:18:20 +0000 |
| Subject: | Bug #73862 [Nab]: Accessing protected property and method of sibling class is inconsistent | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-206509@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=73862&edit=1
ID: 73862
User updated by: after89 at gmail dot com
Reported by: after89 at gmail dot com
Summary: Accessing protected property and method of sibling
class is inconsistent
Status: Not a bug
Type: Bug
Package: *General Issues
PHP Version: 7.1.0
Block user comment: N
Private report: N
New Comment:
What is wrong in my code?
Previous Comments:
------------------------------------------------------------------------
[2017-01-11 05:01:26] krakjoe@php.net
There are errors or assumptions that are wrong in your code.
------------------------------------------------------------------------
[2017-01-05 01:15:09] 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-04 19:07:20] 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-04 19:05:55] 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();
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=73862&edit=1