rfc:use-static-function

Differences

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

Link to this comparison view

Next revision
Previous revision
rfc:use-static-function [2017/01/26 17:32] – created mindplayrfc: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(); 
 +
 +</code> 
 + 
 +The following code imports and calls the ''Foo::bar()'' function by introducing an alias: 
 + 
 +<code php> 
 +use Foo::bar; 
 + 
 +bar(); // calls Foo::bar() 
 +</code> 
 + 
 +Note that aliases (consistent with class-aliases) have a file-level scope - the function ''Foo::bar()'' will be callable as ''bar()'' only within the context of this file. 
 + 
 +The following code imports and calls the ''Foo::bar()'' function by introducing a named alias: 
 + 
 +<code php> 
 +use Foo::bar as plum; 
 + 
 +plum(); // calls Foo::bar() 
 +</code> 
 + 
 +The following code imports and calls two functions by introducing two aliases: 
 + 
 +<code php> 
 +use function Foo::{ bar, baz }; 
 + 
 +bar(); 
 +baz(); 
 +</code> 
 + 
 +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(); 
 +</code> 
 + 
 +Assuming another class ''Nib'' with a public static function ''bar'', the following code imports both ''Foo::bar()'' and ''Nib::bar()'' with two distinct aliases: 
 + 
 +<code php> 
 +use function Foo::bar as a, Nib::bar as b; 
 + 
 +a(); // calls Foo::bar() 
 +b(); // calls Nib::bar() 
 +</code> 
 + 
 +===== How it works ===== 
 + 
 +The net effect of a statement such as ''use Foo::bar'' is virtually equivalent to: 
 + 
 +<code php> 
 +function bar(...$args) { 
 +    return Foo::bar(...$args); 
 +
 +</code> 
 + 
 +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, a call to an alias ''bar()'' is literally equivalent to calling the static method - no evidence of the local alias is visible in a stack-trace or anywhere else, similar to how a class-aliases are not literal symbols. 
 + 
 +In terms of reflection, a statement such as ''new ReflectionFunction("bar")'' would fail, because this alias represents a static method and not a function. A statement such as ''new ReflectionMethod("bar")'' is also not expect to work. To obtain a method-reflection, one needs to use ''new ReflectionMethod(Foo::class, "bar")'' as normal. 
 + 
 +In terms of dynamic resolution, the following are all expected to work: 
 + 
 +<code php> 
 +use Foo::bar; 
 + 
 +call_user_func("bar"); 
 + 
 +array_map($callback, "bar"); 
 +</code> 
 + 
 +===== Alternative Approach ===== 
 + 
 +Alternatively to the above, and perhaps simpler, the ''use'' statements simply introduce a literal function, in the current namespace, that delegates calls to a given static function. 
 + 
 +The advantage is perhaps more obvious use (and/or simpler implementation) of ''call_user_func()'', ''array_map()'' et al. - as well as e.g. ''new ReflectionFunction("bar")'' actually returning a reflection of the delegate function. 
 + 
 +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 ''call_user_func'' ==== 
- 
-TBD 
- 
-==== To ''Reflection'' ==== 
- 
-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 "static classes" might provide a means of grouping functions in such a way that the containing class cannot have state, can only have static methods, cannot extend another class, cannot be extended, and cannot implement interface or use traits.
  
 ===== Proposed Voting Choices ===== ===== Proposed Voting Choices =====
rfc/use-static-function.1485451979.txt.gz · Last modified: 2017/09/22 13:28 (external edit)