rfc:use-static-function
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
rfc:use-static-function [2017/01/26 17:32] – created mindplay | rfc:use-static-function [2017/09/22 13:28] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 25: | Line 25: | ||
Assume for the following examples a class as follows: | Assume for the following examples a class as follows: | ||
- | TODO | + | <code php> |
+ | class Foo | ||
+ | { | ||
+ | public static function bar(); | ||
+ | public static function baz(); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | The following code imports and calls the '' | ||
+ | |||
+ | <code php> | ||
+ | use Foo::bar; | ||
+ | |||
+ | bar(); // calls Foo:: | ||
+ | </ | ||
+ | |||
+ | Note that aliases (consistent with class-aliases) have a file-level scope - the function '' | ||
+ | |||
+ | The following code imports and calls the '' | ||
+ | |||
+ | <code php> | ||
+ | use Foo::bar as plum; | ||
+ | |||
+ | plum(); // calls Foo:: | ||
+ | </ | ||
+ | |||
+ | The following code imports and calls two functions by introducing two aliases: | ||
+ | |||
+ | <code php> | ||
+ | use function Foo::{ bar, baz }; | ||
+ | |||
+ | bar(); | ||
+ | baz(); | ||
+ | </ | ||
+ | |||
+ | The following code imports two functions by introducing named aliases for both: | ||
+ | |||
+ | <code php> | ||
+ | use function Foo::{ bar as blip, baz as plum }; | ||
+ | |||
+ | blip(); | ||
+ | plum(); | ||
+ | </ | ||
+ | |||
+ | Assuming another class '' | ||
+ | |||
+ | <code php> | ||
+ | use function Foo::bar as a, Nib::bar as b; | ||
+ | |||
+ | a(); // calls Foo:: | ||
+ | b(); // calls Nib:: | ||
+ | </ | ||
+ | |||
+ | ===== How it works ===== | ||
+ | |||
+ | The net effect of a statement such as '' | ||
+ | |||
+ | <code php> | ||
+ | function bar(...$args) { | ||
+ | return Foo:: | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | However, this operates at the file-level only - an imported function does not pollute the namespace, just as imported classes do not pollute the actual namespace. Function aliases are available only within the file they were declared in. | ||
+ | |||
+ | In terms of error-handling and stack-traces, | ||
+ | |||
+ | In terms of reflection, a statement such as '' | ||
+ | |||
+ | In terms of dynamic resolution, the following are all expected to work: | ||
+ | |||
+ | <code php> | ||
+ | use Foo::bar; | ||
+ | |||
+ | call_user_func(" | ||
+ | |||
+ | array_map($callback, | ||
+ | </ | ||
+ | |||
+ | ===== Alternative Approach ===== | ||
+ | |||
+ | Alternatively to the above, and perhaps simpler, the '' | ||
+ | |||
+ | The advantage is perhaps more obvious use (and/or simpler implementation) of '' | ||
+ | |||
+ | The downside is these function-aliases would likely need to appear in stack-traces. | ||
+ | |||
+ | Another drawback is that these function aliases of course cannot trigger auto-loading if you attempt to invoke them from outside the file that defined it. | ||
+ | |||
+ | This is probably largely an unattractive prospect, but it's described here for completeness. | ||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
Line 36: | Line 125: | ||
===== RFC Impact ===== | ===== RFC Impact ===== | ||
- | |||
- | ==== On '' | ||
- | |||
- | TBD | ||
- | |||
- | ==== To '' | ||
- | |||
- | TBD | ||
==== To SAPIs ==== | ==== To SAPIs ==== | ||
Line 59: | Line 140: | ||
===== Future Scope ===== | ===== Future Scope ===== | ||
- | TBD | + | TBD: how would this play along with an eventual implementation of function autoloading? |
+ | |||
+ | TBD: the introduction of " | ||
===== Proposed Voting Choices ===== | ===== Proposed Voting Choices ===== |
rfc/use-static-function.1485451979.txt.gz · Last modified: 2017/09/22 13:28 (external edit)