rfc:closures

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
rfc:closures [2008/07/05 14:37]
chris_se added bc break pointed out by Johannes Schlüter
rfc:closures [2017/09/22 13:28] (current)
Line 3: Line 3:
   * Date: 2008-07-01   * Date: 2008-07-01
   * Author: Christian Seiler <chris_se@gmx.net>, Dmitry Stogov <dmitry@zend.com>   * Author: Christian Seiler <chris_se@gmx.net>, Dmitry Stogov <dmitry@zend.com>
-  * Status: Under Discussion+  * Status: Implemented
  
 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. Imaging 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:<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: +
- +
-<code php>+
    function replace_spaces ($text) {    function replace_spaces ($text) {
      if (!function_exists ('replace_spaces_helper')) {      if (!function_exists ('replace_spaces_helper')) {
Line 35: Line 30:
    }    }
 </code> </code>
- +  - Use the present create_function() in order to create a function at runtime. This approach has several disadvantages: First of all, syntax highlighting does not work because a string is passed to the function. It also compiles the function at run time and not at compile time so opcode caches can't cache the function.
-   - Use the present create_function() in order to create a function at runtime. This approach has several disadvantages: First of all, syntax highlighting does not work because a string is passed to the function. It also compiles the function at run time and not at compile time so opcode caches can't cache the function. +
 ==== Closures ==== ==== Closures ====
  
 Closures provide a very useful tool in order to make lambda functions even more useful. Just imagine you want to replace 'hello' through 'goodbye' in all elements of an array. PHP provides the array_map() function which accepts a callback. If you don't want to hard-code 'hello' and 'goodbye' into your sourcecode, you have only four choices: Closures provide a very useful tool in order to make lambda functions even more useful. Just imagine you want to replace 'hello' through 'goodbye' in all elements of an array. PHP provides the array_map() function which accepts a callback. If you don't want to hard-code 'hello' and 'goodbye' into your sourcecode, you have only four choices:
- +  - 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.
- 
 ===== 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.    - 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.    - 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.
  
Line 61: Line 48:
  
 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 83:
      return preg_replace_callback ('/( +) /', $replacement, $text);      return preg_replace_callback ('/( +) /', $replacement, $text);
    }    }
 +</code>
 +
 +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], ' ', '&nbsp;').' ';
 +      }, $text);
 +  }
 </code> </code>
  
Line 120: Line 117:
        }        }
      };      };
-     return array_map ($map, array);+     return array_map ($map, $array);
    }    }
 </code> </code>
Line 162: Line 159:
  
 === Interaction with OOP === === Interaction with OOP ===
 +
 +$this support has been removed, see [[rfc/closures/removal-of-this|removal of this]]
  
 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 200: Line 199:
          $x = 4;          $x = 4;
          $closure = static function ($y) use ($x) {          $closure = static function ($y) use ($x) {
-           return $x + $y;+           return $x + $y;
          };          };
          return $closure (6);          return $closure (6);
Line 208: Line 207:
  
 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 223:
 </code> </code>
  
-==== Interaction with reflection ====+==== Interaction with reflection (1) ====
  
 Since closures are anonymous, they do **not** appear in reflection. Since closures are anonymous, they do **not** appear in reflection.
Line 256: Line 256:
 $object->x = 5; $object->x = 5;
 $closure (); $closure ();
 +</code>
 +
 +==== 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, '__invoke');
 +Reflection::export ($m);
 +</code>
 +
 +This will yield:
 +
 +<code>
 +Method [ <internal> public method __invoke ] {
 +
 +  - Parameters [3] {
 +    Parameter #0 [ <required> $a ]
 +    Parameter #1 [ <required> &$b ]
 +    Parameter #2 [ <optional> $c ]
 +  }
 +}
 +</code>
 +
 +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, 'a');
 +$p = new ReflectionParameter (array ($closure, '__invoke'), 0);
 </code> </code>
  
Line 279: Line 311:
  
 ==== The patch ==== ==== The patch ====
 +
 +**Note:** The patches were already applied to PHP_5_3 and HEAD (with some minor modifications and fixes).
  
 Current patches: Current patches:
Line 312: Line 346:
  
 As the discussion on the mailing list showed, there were quite a few misconceptions on what closures may or may not achieve. One often used suggestion was to use closures in order to extend classes by additional methods at run time. This is **not** the goal of closures **and** it can already be achieved without closures just by using _ _call, see for example [[http://phpfi.com/328105]]. As the discussion on the mailing list showed, there were quite a few misconceptions on what closures may or may not achieve. One often used suggestion was to use closures in order to extend classes by additional methods at run time. This is **not** the goal of closures **and** it can already be achieved without closures just by using _ _call, see for example [[http://phpfi.com/328105]].
 +
 +===== Example code =====
 +
 +The example code in this document is available [[http://www.christian-seiler.de/temp/php-5.3-test-closures.txt|here]].
  
 ===== 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-07-01 Christian Seiler: Updated patch yet again
    * 2008-06-26 Christian Seiler: Revised patch, using objects instead of resources, added tests    * 2008-06-26 Christian Seiler: Revised patch, using objects instead of resources, added tests
Line 322: Line 362:
    * 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.1215268630.txt.gz · Last modified: 2017/09/22 13:28 (external edit)