rfc:use_function

Differences

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

Link to this comparison view

Next revision
Previous revision
rfc:use_function [2013/05/02 20:45] – created igorwrfc:use_function [2017/09/22 13:28] (current) – external edit 127.0.0.1
Line 1: Line 1:
-====== PHP RFC: use function ======+====== PHP RFC: Importing namespaced functions ======
  
-  * Version: 0.1+  * Version: 1.1.1
   * Date: 2013-05-02   * Date: 2013-05-02
   * Author: Igor Wiedler, igor@wiedler.ch   * Author: Igor Wiedler, igor@wiedler.ch
-  * Status: Draft+  * Status: Implemented (PHP-5.6)
   * First Published at: http://wiki.php.net/rfc/use_function   * First Published at: http://wiki.php.net/rfc/use_function
  
Line 50: Line 50:
 ===== Proposal ===== ===== Proposal =====
  
-The proposal is to introduce a new keyword sequence in order to import functions into a namespace. This should make namespaced functions less of a pain to use and discourage placing them in the global namespace.+The proposal is to combine existing keywords to a new sequence that allows importing functions into a namespace. This should make namespaced functions less of a pain to use and discourage placing them in the global namespace.
  
 Since functions and classes are in separate namespaces, it is not feasible to use the **use** keyword for both, as it would likely result in conflicts and overhead. Since functions and classes are in separate namespaces, it is not feasible to use the **use** keyword for both, as it would likely result in conflicts and overhead.
Line 70: Line 70:
     var_dump(baz());     var_dump(baz());
     var_dump(qux());     var_dump(qux());
 +}
 +</code>
 +
 +All of this applies not only to functions, but also to namespaced constants. For consistency, a **use const** sequence should also be introduced, that does the same thing for constants:
 +
 +<code php>
 +namespace foo\bar {
 +    const baz = 42;
 +}
 +
 +namespace {
 +    use const foo\bar\baz;
 +    var_dump(baz);
 +}
 +</code>
 +
 +Just like classes, it should be possible to alias imported functions and constants:
 +
 +<code php>
 +namespace {
 +    use function foo\bar as foo_bar;
 +    use const foo\BAZ as FOO_BAZ;
 +    var_dump(foo_bar());
 +    var_dump(FOO_BAZ);
 } }
 </code> </code>
Line 122: Line 146:
  
 The call to **strlen** is no longer ambiguous. **non_existent** is no longer looked up in the global namespace. The call to **strlen** is no longer ambiguous. **non_existent** is no longer looked up in the global namespace.
 +
 +==== Why is "use function" needed instead of just "use"? ====
 +
 +In PHP, functions and classes are stored in separate namespaces. A function `foo\bar` and a class `foo\bar` can co-exist, because it is possible to infer from the context if the symbol is used as a class or a function:
 +
 +<code php>
 +namespace foo {
 +    function bar() {}
 +    class bar {}
 +}
 +
 +namespace {
 +    foo\bar(); // function call
 +    new foo\bar(); // class instantiation
 +    foo\bar::baz(); // static method call on class
 +}
 +</code>
 +
 +If **use** were changed to support functions as well, it would introduce BC breaks.
 +
 +An example:
 +
 +<code php>
 +namespace {
 +    function bar() {}
 +}
 +
 +namespace foo {
 +    function bar() {}
 +}
 +
 +namespace {
 +    use foo\bar;
 +    bar();
 +}
 +</code>
 +
 +The behaviour changed, when **use** was changed. Depending on your PHP version, a different function will be called.
 +
 +==== Function autoloading ====
 +
 +Function autoloading is beyond the scope of this RFC.
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
Line 153: Line 219:
 ===== Patches and Tests ===== ===== Patches and Tests =====
  
-None yet. +There is a patch against PHP-5.6 (currently master) [[https://github.com/php/php-src/pull/526|as a GitHub pull request]].
 ===== References ===== ===== References =====
  
Line 162: Line 227:
  
 - None. - None.
 +
 +===== Vote =====
 +
 +The voting period is 15.08.2013 until 29.08.2013.
 +
 +<doodle title="RFC/use_function" auth="user" voteType="multi" closed="true">
 +   * Yes?
 +   * No?
 +</doodle>
 +===== Changelog =====
 +
 +  * 2013-08-08 1.1.1 Added example of aliasing
 +  * 2013-07-23 1.1.0 Added support for constants with `use const`
 +  * 2013-07-22 1.0.1 FAQ "why 'use function'?"
 +  * 2013-07-19 1.0.0 First version published for discussion
rfc/use_function.1367527542.txt.gz · Last modified: 2017/09/22 13:28 (external edit)