====== Request for Comments: Enhance Namespace Batch Importing Syntax ====== * Version: 1.0 * Date: 2012-07-24 * Author: Reeze Xia * Status: Under Discussion * First Published at: https://wiki.php.net/rfc/namespace-importing-with-from ===== Introduction ===== This RFC proposes adding a new syntax from TopNamespace use Subnamespace as alias, Subnamespace2, Subnamespace3 as alias3; This syntax is used to import multiple symbols from a common namespace cleanly. It will make namespace importing easier and reduce deplication. ===== Use case ===== When importing multiple class/constant/function from a top namespace, we have to duplicate the top level namespace multiple times. 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: It will 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. but it didn't reduce duplication indeed. ===== 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 enable develpers import several symbols from the same namespace once. 'topnamespace' could be considered as a prefix to every follow use statement: 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. ===== More examples ===== --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=== ===== Patch ===== * Patch located here: https://github.com/reeze/php-src/compare/rfc-from-use ===== Changelog ===== * 2012-07-24 Initially created by Reeze Xia