This is an old revision of the document!

Request for Comments: Enhance Namespace Importing With From Syntax


This RFC proposes adding a new

from namespace use subnamespace as alias

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.

use Top\A\Long\Namespaced\ClassA;
use Top\A\Long\Namespaced\ClassB;
use Top\A\Long\Namespaced\ClassC;

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:

from Top\A\Long\Namespaced\NS use ClassA, ClassB, ClassC;

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.

use Top\Namespaced\ClassA, Top\Namespaced\ClassB, Top\Namespaced\ClassC;

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:

from Top\Namespaced use ClassA, ClassB, ClassC;
//equals to
// we can also import different level
from \GloalNS\Company\Tools use \Tool1, use ToolCollection\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.

Any BC break?

There should no BC break here.

More examples

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";
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) {



* 2012-07-24 Initially created by Reeze Xia

rfc/namespace-importing-with-from.1343125555.txt.gz · Last modified: 2017/09/22 13:28 (external edit)