Bug #74932 [Com]: ascending visibility

From: Date: Sat, 26 May 2018 13:26:52 +0000
Subject: Bug #74932 [Com]: ascending visibility
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-215378@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=74932&edit=1 ID: 74932 Comment by: rowan dot collins at gmail dot com Reported by: vuk dot kulvar+php at gmail dot com Summary: ascending visibility Status: Not a bug Type: Bug Package: Class/Object related Operating System: Windows 10 PHP Version: 7.1.7 Assigned To: cmb Block user comment: N Private report: N New Comment: This is a consequence not of visibility, but of "duck typing". There is no difference in PHP between "redeclaration" or "overriding" and simply adding a new member - all lookups are dynamic at runtime. It may seem odd at first that a parent class can access protected members of a child class at all, but consider this pattern: class A { public function something() { // important logic $this->onSomethingHook($someEventData); // more important logic } protected function onSomethingHook($someEventData) { // No logic; extension point provided for sub-classes to add custom behaviour } } class B extends A { protected function onSomethingHook($someEventData) { // Custom logic here, which will be called from class A } } At a glance, you might say that A::something() is calling B::onSomethingHook(), but in fact the run-time behaviour would be identical if we declared A::onSomethingHook as abstract, or didn't declare it at all - as far as PHP is concerned, the lookup is against whatever $this resolves to at run-time. If we required a declaration of onSomethingHook in A (or its ancestors), would we also require it with a public member? If not, why not? class A { public function something() { $this->onSomethingHook($someEventData); } } class B extends A { public function onSomethingHook($someEventData) { // ... } } Previous Comments: ------------------------------------------------------------------------ [2017-07-21 18:42:58] cmb@php.net This is expected, but currently undocumented[1] and maybe undesirable behavior. I don't think we can change that in a minor version. Related to bug #50892. [1] Some stupid broke the documentation in <http://svn.php.net/viewvc?view=revision&revision=337719> Fixed with <http://svn.php.net/viewvc?view=revision&revision=342692> ------------------------------------------------------------------------ [2017-07-21 18:40:38] cmb@php.net Automatic comment from SVN on behalf of cmb Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=342692 Log: Reverted revision(s) 337719 from phpdoc/en/trunk/language/oop5/visibility.xml: Protected members cannot be accessed by parent classes -- Provided by anonymous 62572 (tunght13488@gmail.com) ........ If we like it or not, that's how it is. See also bug #74932. ------------------------------------------------------------------------ [2017-07-16 22:17:35] vuk dot kulvar+php at gmail dot com Description: ------------ When a parent class has a static method. From the static method of the parent class, you can access protected methods & properties added from the child class. I'm not talking about redeclaration, but methods & properties that doesn't exists in the parent class. Test script: --------------- class Alpha { public static function Render($object) { echo $foo->property; echo $object->method(); } } class Omega extends Alpha { protected $property = 'Property'; protected function method() { return 'Method'; } } Alpha::Render(new Omega()); Expected result: ---------------- Fatal error: Cannot access protected property Omega::$property Fatal error: Call to protected method Omega::method() Actual result: -------------- Property Method ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=74932&edit=1

« previous php.bugs (#215378) next »