rfc:closure_apply
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:closure_apply [2014/07/29 23:23] – Fixed GitHub link (oops!) ajf | rfc:closure_apply [2014/08/17 23:00] – Updated perf stats ajf | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== PHP RFC: Closure::apply ====== | + | ====== PHP RFC: Closure::call ====== |
- | * Version: 0.1 | + | * Version: 0.3 |
- | * Date: 2014-07-29 | + | * Date: 2014-07-29, put to internals 2014-08-03, latest 2014-08-17 |
* Author: Andrea Faulds, ajf@ajf.me | * Author: Andrea Faulds, ajf@ajf.me | ||
- | * Status: | + | * Status: |
* First Published at: http:// | * First Published at: http:// | ||
===== Introduction ===== | ===== Introduction ===== | ||
- | PHP has had Closures since 5.3, and since 5.4 has had '' | + | PHP has had Closures since 5.3, and since 5.4 has had '' |
===== Proposal ===== | ===== Proposal ===== | ||
Line 15: | Line 15: | ||
<code php> | <code php> | ||
- | mixed Closure::apply(object $to[, mixed ...$parameters]) | + | mixed Closure::call(object $to[, mixed ...$parameters]) |
</ | </ | ||
- | It calls the closure with the given parameters and returns the result, with '' | + | It calls the closure with the given parameters and returns the result, with '' |
It can be used like so: | It can be used like so: | ||
Line 26: | Line 26: | ||
$foo-> | $foo-> | ||
$foobar = function ($qux) { var_dump($this-> | $foobar = function ($qux) { var_dump($this-> | ||
- | $foobar-> | + | $foobar-> |
</ | </ | ||
- | Because the '' | + | The '' |
<code php> | <code php> | ||
Line 35: | Line 35: | ||
$foo = new Foo; | $foo = new Foo; | ||
$foobar = function () { var_dump($this-> | $foobar = function () { var_dump($this-> | ||
- | // without the last parameter (optional, defaults to false), we'd get a static, not unbound closure | + | $foobar-> |
- | $foobar = $foobar-> | + | |
- | $foobar-> | + | |
</ | </ | ||
- | My function referencing as closures proposal similarly relaxes the requirement for a scoped closure to be bound or static out of necessity, and '' | + | |
+ | '' | ||
+ | |||
+ | ===== Performance ===== | ||
+ | |||
+ | While not the sole benefit of this RFC, it can provide | ||
+ | |||
+ | We use two test scripts, '' | ||
+ | |||
+ | <file php a.php> | ||
+ | $a = function () { | ||
+ | return $this-> | ||
+ | }; | ||
+ | class FooBar { | ||
+ | private $x = 3; | ||
+ | } | ||
+ | $foobar = new FooBar; | ||
+ | for ($i = 0; $i < 1000000; $i++) { | ||
+ | $x = $a-> | ||
+ | $x(); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | <file php b.php> | ||
+ | $a = function () { | ||
+ | return $this-> | ||
+ | }; | ||
+ | class FooBar { | ||
+ | private $x = 3; | ||
+ | } | ||
+ | $foobar = new FooBar; | ||
+ | for ($i = 0; $i < 1000000; $i++) { | ||
+ | $a-> | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | '' | ||
+ | |||
+ | < | ||
+ | andreas-air: | ||
+ | |||
+ | real 0m1.877s | ||
+ | user 0m1.835s | ||
+ | sys 0m0.025s | ||
+ | |||
+ | andreas-air: | ||
+ | |||
+ | real 0m0.859s | ||
+ | user 0m0.826s | ||
+ | sys 0m0.018s | ||
+ | </ | ||
===== Backward Incompatible Changes and RFC Impact ===== | ===== Backward Incompatible Changes and RFC Impact ===== | ||
Line 54: | Line 102: | ||
Partial application (where a new closure is returned that pre-fills the first X arguments) is a possibly worthwhile (though more difficult to implement) addition. | Partial application (where a new closure is returned that pre-fills the first X arguments) is a possibly worthwhile (though more difficult to implement) addition. | ||
- | ===== Proposed Voting Choices | + | ===== Vote ===== |
This is not a language change, so a straight 50%+1 Yes/No vote can be held. | This is not a language change, so a straight 50%+1 Yes/No vote can be held. | ||
+ | |||
+ | Voting started 2014-08-17 but was cancelled the same day due to the removal of unbound scoped closures. | ||
===== Patches and Tests ===== | ===== Patches and Tests ===== | ||
A branch which implements this (with a test) based on the current master can be found here: https:// | A branch which implements this (with a test) based on the current master can be found here: https:// | ||
+ | |||
+ | There is a pull request for review purposes here: https:// | ||
===== References ===== | ===== References ===== | ||
- | * As aforementioned, my [[rfc: | + | * As previously mentioned, my [[rfc: |
+ | |||
+ | ===== Changelog ===== | ||
+ | |||
+ | * v0.3 - Removed unbound scoped closures, made '' | ||
+ | * v0.2.1 - Added performance section | ||
+ | * v0.2 - '' | ||
+ | * v0.1 - Initial version |
rfc/closure_apply.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1