rfc:namespace-importing-with-from

Differences

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

Link to this comparison view

Next revision
Previous revision
rfc:namespace-importing-with-from [2012/07/24 10:25]
reeze created
rfc:namespace-importing-with-from [2012/09/11 17:43]
reeze prepare cleanup
Line 1: Line 1:
-====== Request for Comments: Enhance Namespace Importing ​With From Syntax ======+====== Request for Comments: Enhance Namespace ​Batch Importing Syntax ======
   * Version: 1.0   * Version: 1.0
   * Date: 2012-07-24   * Date: 2012-07-24
-  * Author: Reeze Xia <reeze.xia@gmail.com>+  * Author: Reeze Xia <reeze@php.net>
   * Status: Under Discussion   * Status: Under Discussion
   * First Published at: https://​wiki.php.net/​rfc/​namespace-importing-with-from   * First Published at: https://​wiki.php.net/​rfc/​namespace-importing-with-from
Line 8: Line 8:
 ===== Introduction ===== ===== Introduction =====
  
-This RFC proposes adding a new <code php>​from ​namespace ​use subnamespace ​as alias</​code> ​syntax +This RFC proposes adding a new syntax ​<code php>​from ​TopNamespace ​use Subnamespace ​as alias, Subnamespace2,​ Subnamespace3 as alias3;</​code>​ 
-to help reduce deplication when importing class/​function/​constants ​from namespace.+This syntax is used to import multiple symbols ​from a common ​namespace ​cleanly. 
 +It will make namespace importing easier and reduce deplication.
  
 ===== Use case ===== ===== Use case =====
Line 17: Line 18:
  
 <code php> <code php>
-use Top\A\Long\Namespaced\ClassA+<?php 
-use Top\A\Long\Namespaced\ClassB+use GlobalNamespace\SubSpace\ThirdSpace\Class1
-use Top\A\Long\Namespaced\ClassC;+use GlobalNamespace\SubSpace\ThirdSpace\Class2
 +use GlobalNamespace\SubSpace\ThirdSpace\ForthSpace\Class3;
 </​code>​ </​code>​
  
Line 26: Line 28:
 This RFC proposes a new syntax to allow developers reduce duplication as below: This RFC proposes a new syntax to allow developers reduce duplication as below:
 <code php> <code php>
-from Top\A\Long\Namespaced\NS ​use ClassAClassBClassC;+<?php 
 +from GlobalNamespace\SubSpace\ThirdSace ​use Class1Class2ForthSpace\Class3;
 </​code>​ </​code>​
  
-It's considered to be an equivalent to the previous multiple use statements.+It will be an equivalent to the previous multiple use statements.
 This makes code less and easier to maintain. This makes code less and easier to maintain.
  
 We could import by combine multiple use statement to a single line. We could import by combine multiple use statement to a single line.
 <code php> <code php>
 +<?php
 use Top\Namespaced\ClassA,​ Top\Namespaced\ClassB,​ Top\Namespaced\ClassC;​ use Top\Namespaced\ClassA,​ Top\Namespaced\ClassB,​ Top\Namespaced\ClassC;​
 </​code>​ </​code>​
-but it didn't reduce duplication ​either.+but it didn't reduce duplication ​indeed.
  
 ===== Syntax explain ===== ===== Syntax explain =====
Line 43: Line 47:
  
 The syntax is ''​from topnamespace use subns_class_func_cons [as alias] [, another_subns_class_func_cons [as alias] ...]''​ 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:+In short this syntax enable develpers import several symbols from the same namespace once. 
 + 
 +'topnamespace' ​could be considered as a prefix to every follow use statement:
 <code php> <code php>
-from Top\Namespaced use ClassA, ClassB, ClassC; +<?php 
-//equals to +from Top\Namespaced use ClassA, ClassB, ClassC; // equals(expand) ​to 
-Top\Namespaced\ClassA;​ + 
-Top\Namespaced\ClassB;​ +use Top\Namespaced\ClassA;​ 
-Top\Namespaced\ClassB;​+use Top\Namespaced\ClassB;​ 
 +use Top\Namespaced\ClassB;​
  
-// we can also import ​different ​level +// we can also import ​from a higher ​level 
-from \GloalNS\Company\Tools use \Tool1, use ToolCollection\NewTool as MyTool;+from \GlobalNS\Company\Tools use \Tool1, use SubCollection\NewTool as MyTool;
 </​code>​ </​code>​
  
Line 62: Line 68:
  
 ==== Why ''​from''?​ ==== ==== Why ''​from''?​ ====
 +
 It was inspired by Python, it use ''​from xxx import yyy as zzz'', ​ It was inspired by Python, it use ''​from xxx import yyy as zzz'', ​
-it's easy to understand what ''​from''​ meanseg: "use those classes from the namespace"​.+it's easy to understand what ''​from''​ meanseg: "use those classes from the namespace"​.
 since PHP use ''​use''​ keyword to import symbol, ​ since PHP use ''​use''​ keyword to import symbol, ​
 so ''​from namespace_ns use sub_namespace_or_symbol as alias''​ is choosen. so ''​from namespace_ns use sub_namespace_or_symbol as alias''​ is choosen.
  
-==== Can we use * to import? ====+==== Can we ''​use *'' ​to import? ====
 No '​use'​ itself didn't and from use syntax didn't either. No '​use'​ itself didn't and from use syntax didn't either.
- 
-==== Any BC break? ==== 
-There should no BC break here. 
  
 ===== More examples ===== ===== More examples =====
Line 97: Line 101:
      * use A\B as B2;      * use A\B as B2;
      */      */
- from A use B as B1, B as B2;+ from A use B as B1, B as B2, C\X as X0;
  
  /*  /*
Line 148: Line 152:
  
 * 2012-07-24 Initially created by Reeze Xia * 2012-07-24 Initially created by Reeze Xia
- 
rfc/namespace-importing-with-from.txt · Last modified: 2017/09/22 13:28 (external edit)