rfc:namespace-importing-with-from

Differences

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

Link to this comparison view

rfc:namespace-importing-with-from [2012/07/24 10:25]
reeze created
rfc:namespace-importing-with-from [2017/09/22 13:28]
Line 1: Line 1:
-====== Request for Comments: Enhance Namespace Importing With From Syntax ====== 
-  * Version: 1.0 
-  * Date: 2012-07-24 
-  * Author: Reeze Xia <​reeze.xia@gmail.com>​ 
-  * Status: Under Discussion 
-  * First Published at: https://​wiki.php.net/​rfc/​namespace-importing-with-from 
- 
-===== Introduction ===== 
- 
-This RFC proposes adding a new <code php>from namespace use subnamespace as alias</​code>​ syntax 
-to help reduce deplication when importing class/​function/​constants from namespace. 
- 
-===== Use case ===== 
- 
-When importing multiple class/​constant/​function from a top namespace, we have to 
-duplicate the top level namespace multiple times. 
- 
-<code php> 
-use Top\A\Long\Namespaced\ClassA;​ 
-use Top\A\Long\Namespaced\ClassB;​ 
-use Top\A\Long\Namespaced\ClassC;​ 
-</​code>​ 
- 
-This is hard to maintain and type, especially when the namespace is deep. 
- 
-This RFC proposes a new syntax to allow developers reduce duplication as below: 
-<code php> 
-from Top\A\Long\Namespaced\NS use ClassA, ClassB, ClassC; 
-</​code>​ 
- 
-It's considered to be an equivalent to the previous multiple use statements. 
-This makes code less and easier to maintain. 
- 
-We could import by combine multiple use statement to a single line. 
-<code php> 
-use Top\Namespaced\ClassA,​ Top\Namespaced\ClassB,​ Top\Namespaced\ClassC;​ 
-</​code>​ 
-but it didn't reduce duplication either. 
- 
-===== Syntax explain ===== 
- 
-This proposed RFC syntax introduce a new keyword: ''​from''​. 
- 
-The syntax is ''​from topnamespace use subns_class_func_cons [as alias] [, another_subns_class_func_cons [as alias] ...]''​ 
-In short this syntax enabled develpers to import several SYMBOLs from the same namespace once. 
- 
-topnamespace could be considered as a prefix to every follow use statement: 
-<code php> 
-from Top\Namespaced use ClassA, ClassB, ClassC; 
-//equals to 
-Top\Namespaced\ClassA;​ 
-Top\Namespaced\ClassB;​ 
-Top\Namespaced\ClassB;​ 
- 
-// we can also import different level 
-from \GloalNS\Company\Tools use \Tool1, use ToolCollection\NewTool as MyTool; 
-</​code>​ 
- 
-We could also import from global namespace by prefix with backslash. but we could only 
-prefix backslash before the namespace after ''​from'',​ the namespace after ''​use''​ statement 
-could either, but it will not import from global namespace, since it's known to be a subnamespace. 
- 
-==== Why ''​from''?​ ==== 
-It was inspired by Python, it use ''​from xxx import yyy as zzz'', ​ 
-it's easy to understand what ''​from''​ means: eg: "use those classes from the namespace"​. 
-since PHP use ''​use''​ keyword to import symbol, ​ 
-so ''​from namespace_ns use sub_namespace_or_symbol as alias''​ is choosen. 
- 
-==== Can we use * to import? ==== 
-No '​use'​ itself didn't and from use syntax didn't either. 
- 
-==== Any BC break? ==== 
-There should no BC break here. 
- 
-===== More examples ===== 
- 
-<code php> 
-<?php 
- 
-namespace A { 
- class B {} 
-} 
- 
-namespace A\C { 
- class X {} 
- class Y {} 
-} 
- 
-namespace { 
- /* 
-     * use A\B 
-     */ 
- from A use B; 
- 
- /* 
-     * use A\B as B1; 
-     * use A\B as B2; 
-     */ 
- from A use B as B1, B as B2; 
- 
- /* 
-     * use A\C\X; 
-     * use A\C\Y; 
-     */ 
- from A\C use X, Y; 
- 
- /* 
-     * use \A\C\X as X1; 
-     * use \A\C\Y as Y1; 
-     */ 
- from \A\C use \X as X1, Y as Y1; 
- 
- $b = new B(); 
- $b1 = new B1(); 
- $b2 = new B2(); 
- $x = new X(); 
- $x1 = new X1(); 
- $y = new Y(); 
- $y1 = new Y1(); 
- 
- var_dump($b,​ $b1, $b2, $x, $x1, $y, $y1); 
- echo "​===DONE===\n";​ 
-} 
-?> 
---EXPECTF-- 
-object(A\B)#​1 (0) { 
-} 
-object(A\B)#​2 (0) { 
-} 
-object(A\B)#​3 (0) { 
-} 
-object(A\C\X)#​4 (0) { 
-} 
-object(A\C\X)#​5 (0) { 
-} 
-object(A\C\Y)#​6 (0) { 
-} 
-object(A\C\Y)#​7 (0) { 
-} 
-===DONE=== 
-</​code>​ 
- 
-===== Patch ===== 
- 
-* Patch located here: https://​github.com/​reeze/​php-src/​compare/​rfc-from-use 
- 
-===== Changelog ===== 
- 
-* 2012-07-24 Initially created by Reeze Xia 
  
rfc/namespace-importing-with-from.txt · Last modified: 2017/09/22 13:28 (external edit)