rfc:namespace-importing-with-from

Request for Comments: Enhance Namespace Batch Importing Syntax

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.

<?php
use GlobalNamespace\SubSpace\ThirdSpace\Class1;
use GlobalNamespace\SubSpace\ThirdSpace\Class2;
use GlobalNamespace\SubSpace\ThirdSpace\ForthSpace\Class3;

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:

<?php
from GlobalNamespace\SubSpace\ThirdSace use Class1, Class2, ForthSpace\Class3;

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.

<?php
use Top\Namespaced\ClassA, Top\Namespaced\ClassB, Top\Namespaced\ClassC;

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:

<?php
from Top\Namespaced use ClassA, ClassB, ClassC; // equals(expand) to
 
use Top\Namespaced\ClassA;
use Top\Namespaced\ClassB;
use Top\Namespaced\ClassB;
 
// we can also import from a higher level
from \GlobalNS\Company\Tools use \Tool1, use SubCollection\NewTool as MyTool;

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

<?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, C\X as X0;
 
	/*
     * 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===

Patch

Changelog

* 2012-07-24 Initially created by Reeze Xia

rfc/namespace-importing-with-from.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1