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 19:06] – Documented additional reflection improvements (see php-internals) chris_se | rfc:closures [2009/12/15 22:40] – Fix list numbering rquadling | ||
---|---|---|---|
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 16: | Line 16: | ||
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. Imagine for example a piece of code that needs to call preg_replace_callback(). Currently, there are three possibilities to achieve this: | Lambda functions allow the quick definition of throw-away functions that are not used elsewhere. Imagine for example a piece of code that needs to call preg_replace_callback(). Currently, there are three possibilities to achieve this: | ||
- | + | - 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:< |
- | + | ||
- | - 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> | + | |
| | ||
if (!function_exists (' | if (!function_exists (' | ||
Line 35: | Line 30: | ||
} | } | ||
</ | </ | ||
- | + | - 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 ==== | ||
Line 61: | Line 55: | ||
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 90: | ||
| | ||
} | } | ||
+ | </ | ||
+ | |||
+ | 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 166: | ||
=== 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 214: | ||
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 362: | Line 369: | ||
* 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