rfc:closures
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
rfc:closures [2008/08/11 07:32] – lsmith | rfc:closures [2009/03/17 14:27] – Note $this isn't available any more scottmac | ||
---|---|---|---|
Line 3: | Line 3: | ||
* Date: 2008-07-01 | * Date: 2008-07-01 | ||
* Author: Christian Seiler < | * Author: Christian Seiler < | ||
- | * Status: | + | * Status: |
This RFC discusses the introduction of compile-time lambda functions and closures in PHP. | This RFC discusses the introduction of compile-time lambda functions and closures in PHP. | ||
Line 61: | Line 61: | ||
The following proposal and patch implement compile-time lambda functions and closures for PHP while keeping the patch as simple as possible. | The following proposal and patch implement compile-time lambda functions and closures for PHP while keeping the patch as simple as possible. | ||
- | |||
==== Userland perspective ==== | ==== Userland perspective ==== | ||
Line 97: | Line 96: | ||
| | ||
} | } | ||
+ | </ | ||
+ | |||
+ | You can even put the lambda function inline, for example: | ||
+ | |||
+ | <code php> | ||
+ | function replace_spaces ($text) { | ||
+ | return preg_replace_callback ('/( +) /', | ||
+ | function ($matches) { | ||
+ | return str_replace ($matches[1], | ||
+ | }, $text); | ||
+ | } | ||
</ | </ | ||
Line 162: | Line 172: | ||
=== Interaction with OOP === | === Interaction with OOP === | ||
+ | |||
+ | $this support has been removed, see [[rfc/ | ||
If a closure is defined inside an object, the closure has full access to the current object through $this (without the need to import it explicitly) and all private and protected methods of that class. This also applies to nested closures. Example: | If a closure is defined inside an object, the closure has full access to the current object through $this (without the need to import it explicitly) and all private and protected methods of that class. This also applies to nested closures. Example: | ||
Line 208: | Line 220: | ||
In this case, $this is not available inside the closure. This may save a lot of memory if saves many closures that originated in longer needed objects. | In this case, $this is not available inside the closure. This may save a lot of memory if saves many closures that originated in longer needed objects. | ||
+ | |||
==== Additional goody: _ _invoke ==== | ==== Additional goody: _ _invoke ==== | ||
Line 223: | Line 236: | ||
</ | </ | ||
- | ==== Interaction with reflection ==== | + | ==== Interaction with reflection |
Since closures are anonymous, they do **not** appear in reflection. | Since closures are anonymous, they do **not** appear in reflection. | ||
Line 256: | Line 269: | ||
$object-> | $object-> | ||
$closure (); | $closure (); | ||
+ | </ | ||
+ | |||
+ | ==== Interaction with reflection (2) ==== | ||
+ | |||
+ | In addition to the previous patch, reflection support was augmented to support reflecting closure objects and returning the correct function pointer. | ||
+ | |||
+ | <code php> | ||
+ | $closure = function ($a, &$b, $c = null) { }; | ||
+ | $m = new ReflectionMethod ($closure, ' | ||
+ | Reflection:: | ||
+ | </ | ||
+ | |||
+ | This will yield: | ||
+ | |||
+ | < | ||
+ | Method [ < | ||
+ | |||
+ | - Parameters [3] { | ||
+ | Parameter #0 [ < | ||
+ | Parameter #1 [ < | ||
+ | Parameter #2 [ < | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | The following will also work (invoke is implied if no method name is specified): | ||
+ | |||
+ | <code php> | ||
+ | $m = new ReflectionMethod ($closure); | ||
+ | $p = new ReflectionParameter ($closure, 0); | ||
+ | $p = new ReflectionParameter ($closure, ' | ||
+ | $p = new ReflectionParameter (array ($closure, ' | ||
</ | </ | ||
Line 321: | Line 366: | ||
===== Changelog ==== | ===== Changelog ==== | ||
+ | * 2008-08-11 Christian Seiler: Documented additional reflection improvements (see php-internals) | ||
* 2008-07-15 Christian Seiler: Updated status of this RFC | * 2008-07-15 Christian Seiler: Updated status of this RFC | ||
* 2008-07-01 Christian Seiler: Updated patch yet again | * 2008-07-01 Christian Seiler: Updated patch yet again | ||
Line 329: | Line 375: | ||
* 2008-06-16 Christian Seiler: Small changes | * 2008-06-16 Christian Seiler: Small changes | ||
* 2008-06-16 Christian Seiler: Initial creation | * 2008-06-16 Christian Seiler: Initial creation | ||
- |
rfc/closures.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1