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/06/16 18:54] – WTFs -> References: JS behaves same way as patch, so not so big WTF chris_se | rfc:closures [2009/12/15 22:40] – Fix list numbering rquadling | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Request for Comments: Lambda functions and closures ====== | ====== Request for Comments: Lambda functions and closures ====== | ||
- | * Version: 1.0 | + | * Version: 1.2 |
- | * Date: 2008-06-16 | + | * 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 10: | Line 10: | ||
End of 2007 a patch was proposed that would add lambda functions (but without closures) to PHP. During the discussion on the mailing list, several people suggested that without support for closures, lambda functions are not useful enough to add them to PHP. This proposal describes a viable method of adding lambda functions with closure support to PHP. | End of 2007 a patch was proposed that would add lambda functions (but without closures) to PHP. During the discussion on the mailing list, several people suggested that without support for closures, lambda functions are not useful enough to add them to PHP. This proposal describes a viable method of adding lambda functions with closure support to PHP. | ||
+ | |||
+ | The initial posting of this proposal has created quite a bit of discussion on the list. This updated proposal including an updated patch intends to incorporate the result of that discussion. A lot of changes to the original patch by Christian Seiler were made by Dmitry Stogov. | ||
===== Why do we need closures and lambda functions? ===== | ===== Why do we need closures and lambda functions? ===== | ||
Closures and lambda functions can make programming much easier in several ways: | Closures and lambda functions can make programming much easier in several ways: | ||
- | |||
==== Lambda Functions ==== | ==== Lambda Functions ==== | ||
- | Lambda functions allow the quick definition of throw-away functions that are not used elsewhere. | + | Lambda functions allow the quick definition of throw-away functions that are not used elsewhere. |
- | + | - Define the callback function elsewhere. This distributes code that belongs together throughout the file and decreases readability. | |
- | - Define the callback function elsewhere. This distributes code that belongs together throughout the file and decreases readability. | + | - Define the callback function in-place (but with a name). In that case one has to use function_exists() to make sure the function is only defined once. Here, the additional if() around the function definition makes the source code difficult to read. Example code:<code php> |
- | + | ||
- | - Define the callback function in-place (but with a name). In that case one has to use function_exists() to make sure the function is only defined once. Here, the additional if() around the function definition makes the source code difficult to read. Example code: | + | |
- | + | ||
- | <?php | + | |
| | ||
if (!function_exists (' | if (!function_exists (' | ||
Line 32: | Line 29: | ||
| | ||
} | } | ||
- | ?> | + | </code> |
- | + | - Use the present create_function() in order to create a function at runtime. This approach has several disadvantages: | |
- | - Use the present create_function() in order to create a function at runtime. This approach has several disadvantages: | + | |
==== Closures ==== | ==== Closures ==== | ||
- | Closures provide a very useful tool in order to make lambda functions even more useful. Just imagine you want to replace ' | + | Closures provide a very useful tool in order to make lambda functions even more useful. Just imagine you want to replace ' |
- | + | - Use create_function(). But then you may only pass literal values (strings, integers, floats) into the function, objects at best as clones (if var_export() allows for it) and resources not at all. And you have to worry about escaping everything correctly. Especially when handling user input this can lead to all sorts of security issues. | |
- | - Use create_function(). But then you may only pass literal values (strings, integers, floats) into the function, objects at best as clones (if var_export() allows for it) and resources not at all. And you have to worry about escaping everything correctly. Especially when handling user input this can lead to all sorts of security issues. | + | - Write a function that uses global variables. This is ugly, non-reentrant and bad style. |
- | + | - Create an entire class, instantiate it and pass the member function as a callback. This is perhaps the cleanest solution for this problem with current PHP but just think about it: Creating an entire class for this extremely simple purpose and nothing else seems overkill. | |
- | - Write a function that uses global variables. This is ugly, non-reentrant and bad style. | + | - Don't use array_map() but simply do it manually (foreach). In this simple case it may not be that much of an issue (because one simply wants to iterate over an array) but there are cases where doing something manually that a function with a callback as parameter does for you is quite tedious. |
- | + | ||
- | - Create an entire class, instantiate it and pass the member function as a callback. This is perhaps the cleanest solution for this problem with current PHP but just think about it: Creating an entire class for this extremely simple purpose and nothing else seems overkill. | + | |
- | + | ||
- | - Don't use array_map() but simply do it manually (foreach). In this simple case it may not be that much of an issue (because one simply wants to iterate over an array) but there are cases where doing something manually that a function with a callback as parameter does for you is quite tedious. | + | |
Note: str_replace also accepts arrays as a third parameter so this example may be a bit useless. But imagine you want to do a more complex operation than simple search and replace. | Note: str_replace also accepts arrays as a third parameter so this example may be a bit useless. But imagine you want to do a more complex operation than simple search and replace. | ||
Line 52: | Line 43: | ||
===== Common misconceptions ===== | ===== Common misconceptions ===== | ||
- | ? | + | - Lambda functions / closures are **not** a way of dynamically extending classes by additional methods at runtime. There are several other possibilities to do this, including the already present _ _call semantic. |
+ | |||
+ | - PHP's notion of scope is quite different than the notion of scope other languages define. Combine this with variable variables ($$var) and it becomes clear that automatically detecting which variables from the outer scope are referenced inside are closure is impossible. Also, since for example global variables are not visible inside functions either by default, automatically making the parent scope available would break with the current language concept PHP follows. | ||
===== Proposal and Patch ===== | ===== Proposal and Patch ===== | ||
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 64: | Line 56: | ||
The patch adds the following syntax as a valid expression: | The patch adds the following syntax as a valid expression: | ||
- | function & (parameters) { body } | + | <code php> |
+ | function & (parameters) use (lexical vars) { body } | ||
+ | </ | ||
- | (The & is optional and indicates | + | The & is optional and indicates that the function |
Example usage: | Example usage: | ||
+ | <code php> | ||
| | ||
+ | </ | ||
The variable $lambda then contains a callable resource that may be called through different means: | The variable $lambda then contains a callable resource that may be called through different means: | ||
+ | <code php> | ||
| | ||
| | ||
| | ||
+ | </ | ||
This allows for simple lambda functions, for example: | This allows for simple lambda functions, for example: | ||
+ | <code php> | ||
| | ||
| | ||
Line 86: | Line 85: | ||
| | ||
} | } | ||
+ | </ | ||
- | === Closure support via '' | + | You can even put the lambda function inline, for example: |
- | The patch implements closures by defining an additional keyword | + | <code php> |
+ | function replace_spaces ($text) { | ||
+ | return preg_replace_callback ('/( +) /', | ||
+ | | ||
+ | return str_replace ($matches[1], | ||
+ | }, $text); | ||
+ | } | ||
+ | </ | ||
+ | === Closure support === | ||
+ | |||
+ | In order to make use of variables defined in the parent scope, this patch proposes the following syntax to import variables from the parent scope into the closure scope: | ||
+ | |||
+ | <code php> | ||
+ | function (normal parameters) use ($var1, $var2, & | ||
+ | </ | ||
+ | |||
+ | The variables $var1, $var2 and $refvar defined in the parent scope will be visible inside the lambda function. For the behaviour with regard to references, see below. | ||
+ | |||
+ | Simple example: | ||
+ | |||
+ | <code php> | ||
| | ||
- | $map = function ($text) | + | $map = function ($text) |
- | | + | |
if (strpos ($text, $search) > 50) { | if (strpos ($text, $search) > 50) { | ||
| | ||
Line 100: | Line 119: | ||
} | } | ||
}; | }; | ||
- | | + | |
} | } | ||
+ | </ | ||
- | The variables $search and $replacement are variables in the scope of the function replace_in_array() and the lexical keyword imports these variables | + | The variables $search and $replacement are variables in the scope of the function replace_in_array() and they are imported |
- | + | ||
- | === Interaction with OOP === | + | |
- | + | ||
- | If a closure | + | |
=== Closure lifetime === | === Closure lifetime === | ||
Line 113: | Line 129: | ||
Closures may live longer as the methods that declared them. It is perfectly possible to have something like this: | Closures may live longer as the methods that declared them. It is perfectly possible to have something like this: | ||
+ | <code php> | ||
| | ||
- | | + | |
- | | + | // or: lexical $x; |
| | ||
}; | }; | ||
} | } | ||
+ | </ | ||
+ | |||
+ | === References vs. Copies === | ||
+ | |||
+ | By default, all imported variables are copied as values into the closure. This makes it impossible for a closure to modify the variable in the parent scope. By prepending an & in front of the variable name in the use declaration, | ||
+ | |||
+ | Example: | ||
+ | |||
+ | <code php> | ||
+ | $x = 1; | ||
+ | $lambda1 = function () use ($x) { | ||
+ | $x *= 2; | ||
+ | }; | ||
+ | $lambda2 = function () use (&$x) { | ||
+ | $x *= 3; | ||
+ | }; | ||
+ | $lambda1 (); | ||
+ | var_dump ($x); // gives: 1 | ||
+ | $lambda2 (); | ||
+ | var_dump ($x); // gives: 3 | ||
+ | </ | ||
+ | |||
+ | Support for references are necessary in order to achieve true closures (like in Javascript, where a variable originating in parent scope can be modified by closures) while copying per default fits best with the current semantics of PHP and does not cause headaches in loops (for example, when importing a loop index into a closure). | ||
+ | |||
+ | === 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: | ||
+ | |||
+ | <code php> | ||
+ | class Example { | ||
+ | | ||
+ | |||
+ | | ||
+ | | ||
+ | } | ||
+ | |||
+ | | ||
+ | | ||
+ | } | ||
+ | |||
+ | | ||
+ | | ||
+ | | ||
+ | }; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | | ||
+ | | ||
+ | echo $replacer (' | ||
+ | | ||
+ | echo $replacer (' | ||
+ | </ | ||
+ | |||
+ | As one can see, defining a closure inside a class method does not change the semantics at all - it simply does not matter if a closure is defined in global scope, within a function or within a class method. The only small difference is that closures defined in class methods may also access the class and the current object via $this. Since $this is saved " | ||
+ | |||
+ | Because not all closures defined in class methods need $this, it is possible to declare a lambda function to be static: | ||
+ | |||
+ | <code php> | ||
+ | class Example { | ||
+ | | ||
+ | $x = 4; | ||
+ | | ||
+ | | ||
+ | }; | ||
+ | | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | 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 ==== | ||
+ | |||
+ | Since closures implement a new type of variable that may be called dynamically (i.e. objects), the idea came up that generic callable could also be implemented. This patch adds an additional magic method _ _invoke that may be defined in arbitrary classes. If defined, the object itself is callable and the new special method will be invoked instead of the object. Example: | ||
+ | |||
+ | <code php> | ||
+ | class Example { | ||
+ | public function __invoke () { | ||
+ | echo "Hello World!\n"; | ||
+ | } | ||
+ | } | ||
+ | $foo = new Example; | ||
+ | $foo (); | ||
+ | </ | ||
+ | |||
+ | ==== Interaction with reflection (1) ==== | ||
+ | |||
+ | Since closures are anonymous, they do **not** appear in reflection. | ||
+ | |||
+ | However, a new method was added to the ReflectionMethod and ReflectionFunction classes: getClosure. This method returns a dynamically created closure for the specified function. Example: | ||
+ | |||
+ | <code php> | ||
+ | class Example { | ||
+ | static function printer () { echo "Hello World!\n"; | ||
+ | } | ||
+ | |||
+ | $class = new ReflectionClass (' | ||
+ | $method = $class-> | ||
+ | $closure = $method-> | ||
+ | $closure (); | ||
+ | </ | ||
+ | |||
+ | This example dynamically creates a callable object of the static method " | ||
+ | |||
+ | <code php> | ||
+ | class Example { | ||
+ | public $x = 4; | ||
+ | function printer () { echo "Hello World: $this-> | ||
+ | } | ||
+ | |||
+ | $class = new ReflectionClass (' | ||
+ | $method = $class-> | ||
+ | |||
+ | $object = new Example; | ||
+ | $closure = $method-> | ||
+ | $closure (); | ||
+ | $object-> | ||
+ | $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, ' | ||
+ | </ | ||
==== Zend internal perspective ==== | ==== Zend internal perspective ==== | ||
Line 124: | Line 296: | ||
The patch basically changes the following in the Zend engine: | The patch basically changes the following in the Zend engine: | ||
- | When the compiler reaches a lambda function, | + | When the compiler reaches a lambda function, |
- | When parsing a 'lexical' declaration inside | + | Lexical variables are done via static variables: For each lexical |
- | The opcode handler for ZEND_DECLARE_LAMBDA_FUNC does the following: First of all it creates a new op_array and copies the entire memory structure of the lambda function into it (the opcodes themselves are not copied since they are only referenced in the op_array structure). Then it sets is_closure = 1 on the new op_array, and for each lexical variable name that the compiler | + | An additional internal class " |
- | The opcode | + | The ZEND_DECLARE_LAMBDA_FUNCTION |
- | Some hooks were added that allow the 'lambda function' | + | Some hooks were added to the opcode handlers, zend_call_function and zend_is_callable_ex |
+ | |||
+ | In order to make code changes as clean as possible, this logic was mainly abstracted into zend_closures.c which defines two main methods: zend_create_closure and zend_get_closure. zend_create_closure creates a new closure, zend_get_closure retrieves | ||
+ | |||
+ | ==== Tests ==== | ||
+ | |||
+ | The patch contains additional phpt tests that make sure closures work as designed. | ||
==== The patch ==== | ==== The patch ==== | ||
- | The patch for PHP 5.3 is available here: | + | **Note: |
- | * [[http:// | + | Current patches: |
- | A patch for PHP 6 (HEAD) will be added as soon as the unicode_semantics removal is complete. | + | * [[http:// |
+ | * [[http:// | ||
+ | |||
+ | Older patches for completeness: | ||
+ | |||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
**Note** The patch does not contain the diff for '' | **Note** The patch does not contain the diff for '' | ||
Line 146: | Line 336: | ||
==== BC breaks ==== | ==== BC breaks ==== | ||
- | | + | |
- | | + | |
==== Caveats / possible WTFs ==== | ==== Caveats / possible WTFs ==== | ||
Line 153: | Line 343: | ||
=== Trailing '';'' | === Trailing '';'' | ||
- | On writing '' | + | On writing '' |
- | === References | + | === Misinterpretations of the goal of closures |
- | The fact that ' | + | As the discussion on the mailing list showed, there were quite a few misconceptions on what closures |
- | for ($i = 0; $i < 10; $i++) { | + | ===== Example code ===== |
- | | + | |
- | } | + | |
- | This will not work as expected since $i is a reference and thus all created closures would reference the same variable. In order to get this right one has to do: | + | The example code in this document |
- | + | ||
- | for ($i = 0; $i < 10; $i++) { | + | |
- | | + | |
- | | + | |
- | unset ($loopIndex); | + | |
- | } | + | |
- | + | ||
- | This can be a WTF for people that don't expect lexical to create an actual reference. On the other hand, global and static both DO create references so that behaviour is consistent with current PHP **and** (as pointed out on the mailing list) other languages such as JavaScript also behave the same way, so we really should stay consistent. | + | |
- | + | ||
- | === '' | + | |
- | + | ||
- | The fact that ' | + | |
===== 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-01 Christian Seiler: Updated patch yet again | ||
+ | * 2008-06-26 Christian Seiler: Revised patch, using objects instead of resources, added tests | ||
+ | * 2008-06-18 Christian Seiler: OOP clarifications | ||
+ | * 2008-06-17 Christian Seiler: Updated patch | ||
+ | * 2008-06-17 Christian Seiler: Clarified interaction with OOP | ||
* 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