Bug #68194 [Asn->Fbk]: Closure captures late bound class too eagerly

From: Date: Tue, 05 May 2015 17:59:46 +0000
Subject: Bug #68194 [Asn->Fbk]: Closure captures late bound class too eagerly
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-192494@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
 Updated by:         levim@php.net
 Reported by:        denis at slik dot eu
 Summary:            Closure captures late bound class too eagerly
-Status:             Assigned
+Status:             Feedback
 Type:               Bug
 Package:            Scripting Engine problem
 Operating System:   Irrelevant
 PHP Version:        5.6.1
 Assigned To:        levim
 Block user comment: N
 Private report:     N



Previous Comments:
------------------------------------------------------------------------
[2015-05-05 17:59:31] levim@php.net

I have looked at this again and I believe that this is not a bug. Class Right has no permission to
access anything that is protected or private in Left.

------------------------------------------------------------------------
[2014-10-17 14:45:14] levim@php.net

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.

------------------------------------------------------------------------
[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


Thread (10 messages)

« previous php.bugs (#192494) next »