rfc:closure_apply

Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
rfc:closure_apply [2014/08/17 22:55] – Removed voteblock ajfrfc:closure_apply [2014/08/20 14:14] – No longer previously mentioned ajf
Line 1: Line 1:
 ====== PHP RFC: Closure::call ====== ====== PHP RFC: Closure::call ======
   * Version: 0.3   * Version: 0.3
-  * Date: 2014-07-29, put to internals 2014-08-03, latest 2014-08-17+  * Date: 2014-07-29, put to internals 2014-08-03, latest 2014-08-19
   * Author: Andrea Faulds, ajf@ajf.me   * Author: Andrea Faulds, ajf@ajf.me
-  * Status: Under Discussion+  * Status: In Voting
   * First Published at: http://wiki.php.net/rfc/closure_apply   * First Published at: http://wiki.php.net/rfc/closure_apply
  
Line 18: Line 18:
 </code> </code>
  
-It calls the closure with the given parameters and returns the result, with ''$this'' bound to the given object ''$to'', using the closure's current scope. Like the ''bind''(''To'') methods, a static class cannot be bound (using ''->call'' will fail).+It calls the closure with the given parameters and returns the result, with ''$this'' bound to the given object ''$to'', using the scope of the given object. Like the ''bind''(''To'') methods, a static class cannot be bound (using ''->call'' will fail).
  
 It can be used like so: It can be used like so:
Line 34: Line 34:
 class Foo { private $x = 3; } class Foo { private $x = 3; }
 $foo = new Foo; $foo = new Foo;
-$foobar = function () { var_dump($this->bar); };+$foobar = function () { var_dump($this->x); };
 $foobar->call($foo); // prints int(3) $foobar->call($foo); // prints int(3)
 </code> </code>
- 
  
 ''call'' would be useful in many cases where ''bindTo'' is used (e.g. [[https://github.com/search?l=php&p=34&q=bindTo&ref=cmdform&type=Code|search of GitHub for bindTo]]). A search on GitHub reveals [[https://github.com/search?q=bindTo+call_user_func&type=Code&ref=searchresults|many using bindTo and immediately calling with call_user_func]], which would now not be necessary as they could just use ''call''. ''call'' would be useful in many cases where ''bindTo'' is used (e.g. [[https://github.com/search?l=php&p=34&q=bindTo&ref=cmdform&type=Code|search of GitHub for bindTo]]). A search on GitHub reveals [[https://github.com/search?q=bindTo+call_user_func&type=Code&ref=searchresults|many using bindTo and immediately calling with call_user_func]], which would now not be necessary as they could just use ''call''.
Line 55: Line 54:
 } }
 $foobar = new FooBar; $foobar = new FooBar;
-for ($i = 0; $i < 100000; $i++) {+for ($i = 0; $i < 1000000; $i++) {
     $x = $a->bindTo($foobar, "FooBar");     $x = $a->bindTo($foobar, "FooBar");
     $x();     $x();
Line 69: Line 68:
 } }
 $foobar = new FooBar; $foobar = new FooBar;
-for ($i = 0; $i < 100000; $i++) {+for ($i = 0; $i < 1000000; $i++) {
     $a->call($foobar);     $a->call($foobar);
 } }
 </file> </file>
  
-When run for 100000 iterations, ''b.php'' shows a 2x improvement over ''a.php'':+''b.php'' shows a 2.18x improvement over ''a.php'':
  
 <code> <code>
 andreas-air:php-src ajf$ time sapi/cli/php a.php andreas-air:php-src ajf$ time sapi/cli/php a.php
-  + 
-real 0m0.259s +real 0m1.877s 
-user 0m0.208s +user 0m1.835s 
-sys 0m0.012s +sys 0m0.025s 
- +
 andreas-air:php-src ajf$ time sapi/cli/php b.php andreas-air:php-src ajf$ time sapi/cli/php b.php
-  
-real 0m0.100s 
-user 0m0.094s 
-sys 0m0.005s 
-</code> 
-  
-If we up the iterations by 10x, the result is the same: 
  
-<code> +real 0m0.859s 
-andreas-air:php-src ajf$ time sapi/cli/php a.php +user 0m0.826s 
-  +sys 0m0.018s
-real 0m1.966s +
-user 0m1.959s +
-sys 0m0.005s +
-  +
-andreas-air:php-src ajf$ time sapi/cli/php b.php +
-  +
-real 0m0.962s +
-user 0m0.897s +
-sys 0m0.015s+
 </code> </code>
  
Line 112: Line 95:
 ===== Proposed PHP Version(s) ===== ===== Proposed PHP Version(s) =====
  
-This is proposed for the next version of PHP, either the next 5.x or PHP NEXT, whichever comes sooner. The patch is based on master, intended for the next 5.x.+This is proposed for the next version of PHP, either the next 5.x or PHP NEXT, whichever comes sooner. The patch is based on master, intended for PHP 7.
  
 ===== Future Scope ===== ===== Future Scope =====
Line 123: Line 106:
  
 Voting started 2014-08-17 but was cancelled the same day due to the removal of unbound scoped closures. Voting started 2014-08-17 but was cancelled the same day due to the removal of unbound scoped closures.
 +
 +Voting started again on 2014-08-20 and ends 2014-08-27.
 +
 +<doodle title="Closure::apply() (Approve RFC and merge into master?)" auth="ajf" voteType="single" closed="false">
 +   * Yes
 +   * No
 +</doodle>
  
 ===== Patches and Tests ===== ===== Patches and Tests =====
Line 132: Line 122:
 ===== References ===== ===== References =====
  
-  * As previously mentioned, my [[rfc:function_referencing|Function Referencing as Closures]] RFC has this RFC as a prerequisite+  * My [[rfc:function_referencing|Function Referencing as Closures]] RFC has this RFC as a prerequisite
  
 ===== Changelog ===== ===== Changelog =====
rfc/closure_apply.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1