rfc:closures:removal-of-this
Differences
This shows you the differences between two versions of the page.
rfc:closures:removal-of-this [2009/01/26 12:27] cseiler created |
rfc:closures:removal-of-this [2017/09/22 13:28] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Removal of $this in closures ====== | ||
- | (NOTE THAT THIS IS NOT COMMITTED YET. THIS PAGE IS STILL WORK IN PROGRESS) | ||
- | |||
- | This document describes the changes that were done in order to remove $this support from closures for PHP 5.3 beta1. This has become necessary in order to make sure that when a consensus is found on how to add $this to closures, it will be able to integrate that without BC issues. | ||
- | |||
- | ===== Userland perspective ===== | ||
- | |||
- | In the userland perspective, | ||
- | |||
- | <code php> | ||
- | $closure = function () { }; // still works | ||
- | $closure = static function () { }; // DOES NOT WORK | ||
- | class Foo { | ||
- | public $pubMember; | ||
- | private $privMembeR; | ||
- | function bar { | ||
- | $closure = function () { | ||
- | return $this; // DOES NOT WORK | ||
- | }; | ||
- | $closure = static function () { // DOES NOT WORK | ||
- | }; | ||
- | $closure = function () { echo "Hello World!\n"; | ||
- | $closure = function () use ($this) { ... }; // DOES NOT WORK | ||
- | $self = $this; | ||
- | $closure = function () use ($self) { // still works | ||
- | echo $self-> | ||
- | echo $self-> | ||
- | }; | ||
- | } | ||
- | } | ||
- | </ | ||
- | |||
- | Yes, these are quite some restrictions on closures, however, this is the only possibility to ensure that support for $this can be added in any fashion that may later be required. | ||
- | |||
- | ===== Internals perspective ===== | ||
- | |||
- | - zend_closure structure: this_ptr member is gone. | ||
- | - removed zend_get_closure() prototype in zend_closures.h (was not implemented anyway anymore since it's now done via a handler) | ||
- | - removd scope and this_ptr parameters from zend_create_closure() | ||
- | - removed zend_get_closure_this_ptr | ||
- | |||
- | ==== Important note on get_closure handler ==== | ||
- | |||
- | However, the get_closure handler prototype was **NOT** changed (only the implementation for closures, which always set the scope and object ptr to NULL) and still allows the handler implementation to set a scope and this_ptr because the handler is also responsible for __invoke on normal objects! | ||
- | |||
- | ===== Reflection ===== | ||
- | |||
- | TBD. |
rfc/closures/removal-of-this.txt · Last modified: 2017/09/22 13:28 (external edit)