PHP 5.4 - 'closure $this support'

前端 未结 4 1686
生来不讨喜
生来不讨喜 2020-11-29 01:03

I see that the new planned features for PHP 5.4 are: traits, array dereferencing, a JsonSerializable interface and something referred to as \'closure $this support

4条回答
  •  借酒劲吻你
    2020-11-29 01:51

    This was already planned for PHP 5.3, but

    For PHP 5.3 $this support for Closures was removed because no consensus could be reached how to implement it in a sane fashion. This RFC describes the possible roads that can be taken to implement it in the next PHP version.

    It indeed means you can refer to the object instance (live demo)

    value; };
      }
    }
    
    $a = new A;
    $fn = $a->getClosure();
    echo $fn(); // 1
    

    For a discussion, see the PHP Wiki

    • Closures: Object extension

    and for historic interest:

    • closures (rfc)
    • removal-of-this (rfc:closures)

提交回复
热议问题