Bug #68194 [Com]: Closure captures late bound class too eagerly

From: Date: Fri, 17 Oct 2014 14:45:15 +0000
Subject: Bug #68194 [Com]: Closure captures late bound class too eagerly
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-188168@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=68194&edit=1 ID: 68194 Comment by: levim@php.net Reported by: denis at slik dot eu Summary: Closure captures late bound class too eagerly Status: Assigned Type: Bug Package: Scripting Engine problem Operating System: Irrelevant PHP Version: 5.6.1 Assigned To: levim Block user comment: N Private report: N New Comment: This behavior is documented on the visibility page: http://php.net/manual/en/language.oop5.visibility.php > Members declared protected can be accessed only within the class itself and by inherited and > parent classes. With that being said, everyone I have talked to thus far believes it is a bug and should be considered for removal in the next major version of PHP. This needs to be discussed with more people. While that discussion is going on I'll dig into the technical details of why the closure doesn't work. Previous Comments: ------------------------------------------------------------------------ [2014-10-17 13:58:36] levim@php.net To be completely honest, I am not sure why Right and Base can access a protected property in Left. The behavior of the closure actually seems correct to me. I'll have to poke around in documentation and talk to more people. Thank you for this report. ------------------------------------------------------------------------ [2014-10-17 13:18:43] tyrael@php.net Levi, could you look into this please? ------------------------------------------------------------------------ [2014-10-09 06:54:43] denis at slik dot eu Description: ------------ In #66622, Closures have been changed to capture the late bound class very eagerly to make the static keyword work. This had a side effect of backwards-incompatible visibility breaking. For a full example, see http://3v4l.org/O5dLF. This worked in <=5.4.29, <=5.5.13, but breaks in >=5.4.30, >=5.5.14, >=5.6.1. It would seem sensible that refactoring the code inside a closure would not change the visibility restrictions. Test script: --------------- class Base { public static function hi() { echo Left::$prot; $cl = function(){ echo Left::$prot; }; $cl(); } } class Left extends Base { protected static $prot = "works\n"; } class Right extends Base { } Right::hi(); Expected result: ---------------- works works Actual result: -------------- works Fatal error: Cannot access protected property Left::$prot ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=68194&edit=1

« previous php.bugs (#188168) next »