# [RFC] Proposal namespace importing with from syntax

- Thread: https://externals.io/message/61691
- Original archive: https://news-web.php.net/php.internals/61691
- Started by Reeze on 2012-07-24T15:35:03+00:00
- Messages: 11
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #61691 — Reeze — 2012-07-24T15:35:03+00:00

URL: https://externals.io/message/61691#61691

Hi, internals,
I'm proposing a improvement to namespace importing. when using 'use' statement to import
many classes, we have to duplicate many times even  we are importing from the same namespace.

I propose a new syntax to import classes\functions\constant in a single statement.

here is the example:

<?php
// reduce 
use GlobalNamespace\SubSpace\ThirdSpace\Class1;
use GlobalNamespace\SubSpace\ThirdSpace\Class2;
use GlobalNamespace\SubSpace\ThirdSpace\ForthSpace\Class3;
// to
from GlobalNamespace\SubSpace\ThirdSace use Class1, Class2 as Alias2, ForthSpace\Class3 as Alias3;
?>

The above code should be self-explained.

More info could be found at the RFC page and the patch.

RFC: https://wiki.php.net/rfc/namespace-importing-with-from
PATCH :   https://github.com/reeze/php-src/compare/rfc-from-use

Any Opinion, suggestion, improvement will be much appreciated.

Thanks 

-- 
reeze | http://reeze.cn

---

## #61692 — Andrew Faulds — 2012-07-24T15:39:07+00:00

URL: https://externals.io/message/61691#61692 · In reply to: #61691

> Hi, internals,
> I'm proposing a improvement to namespace importing. when using 'use' statement to import
> many classes, we have to duplicate many times even  we are importing from the same namespace.
>
> I propose a new syntax to import classes\functions\constant in a single statement.
>
> here is the example:
>
> <?php
> // reduce
> use GlobalNamespace\SubSpace\ThirdSpace\Class1;
> use GlobalNamespace\SubSpace\ThirdSpace\Class2;
> use GlobalNamespace\SubSpace\ThirdSpace\ForthSpace\Class3;
> // to
> from GlobalNamespace\SubSpace\ThirdSace use Class1, Class2 as Alias2, ForthSpace\Class3 as Alias3;
> ?>
>
> The above code should be self-explained.
>
> More info could be found at the RFC page and the patch.
>
> RFC: https://wiki.php.net/rfc/namespace-importing-with-from
> PATCH :   https://github.com/reeze/php-src/compare/rfc-from-use
>
> Any Opinion, suggestion, improvement will be much appreciated.
>
> Thanks
>
Ah, so something a la python's from x import y, z? Sounds good. Somewhat 
surprised we don't already have this.

One comment: "use Class1, Class2 as Alias2, ... ;" confuses me a bit. I 
read it as "use (Class1, Class2) as Alias2, ... ;". //Perhaps this is 
just a formatting issue (with newlines it would be more clear), but it 
looks a little weird to me.

Just my 2¢.

-- 
Andrew Faulds
http://ajf.me/

---

## #61693 — Reeze — 2012-07-24T15:48:57+00:00

URL: https://externals.io/message/61691#61693 · In reply to: #61692

Hi Andrew,

在 2012年7月24日星期二，下午11:39，Andrew Faulds 写道：

> 
> > […]
> Ah, so something a la python's from x import y, z? Sounds good. Somewhat  
> surprised we don't already have this.
>  
> One comment: "use Class1, Class2 as Alias2, ... ;" confuses me a bit. I  
> read it as "use (Class1, Class2) as Alias2, ... ;". //Perhaps this is
>  
>  

It means use Class1 **and** use Class2 but aliased with name Alias2 etc.
It almost the same as bare 'use' statement, but with a common prefix :)

Thanks
>  
> just a formatting issue (with newlines it would be more clear), but it  
> looks a little weird to me.
>  
> Just my 2¢.
>  
> --  
> Andrew Faulds
> http://ajf.me/

---

## #61718 — Stas Malyshev — 2012-07-24T18:33:11+00:00

URL: https://externals.io/message/61691#61718 · In reply to: #61691

Hi!

> from GlobalNamespace\SubSpace\ThirdSace use Class1, Class2 as Alias2, ForthSpace\Class3 as Alias3;

I'm not sure it's necessary. If you import a real lot of the classes
from the same namespace, just import the parent namespace. And this
syntax makes less clear what is the true name of Class2 and also by
similarity to python syntax would lead people to think it does the same
thing python one does - which it is not.


-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227

---

## #61731 — Reeze Xia — 2012-07-25T02:43:33+00:00

URL: https://externals.io/message/61691#61731 · In reply to: #61718

Hi Stas,  

在 2012年7月25日星期三，上午2:33，Stas Malyshev 写道：

> Hi!
>  
> > […]
>  
> I'm not sure it's necessary. If you import a real lot of the classes
> from the same namespace, just import the parent namespace. And this
>  
>  

No, we can not import namespace directly for now :
<?php
namespace A {
    class B {}
}

namespace {
use A;

var_dump(new B());
}

Warning: The use statement with non-compound name 'A' has no effect in /Users/reeze/Opensource/php-test/php-src-master/a.php on line 7

if we wan to archive the goal of import anything. the only way is to declare the current namespace  
as  "the parent namespace" we want to import from, eg:

<?php
namespace Zend {
    class B {}
}

namespace *Zend* {

var_dump(new B());
}

but this is not preferred for use code declared as the library we used.
> syntax makes less clear what is the true name of Class2 and also by
> similarity to python syntax would lead people to think it does the same
> thing python one does - which it is not.
>  
>  

Sorry, I didn't make myself clear. the example I posted is not correct. It didn't have to alias the imported classes.
'use' statement can alias the imported class too, the proposed 'from xx use yy' is almost  
the same as 'use'

The correct example should be:

<?php
// if we don't want to duplicated then the follow could be
use GlobalNamespace\SubSpace\ThirdSpace\Class1;
use GlobalNamespace\SubSpace\ThirdSpace\Class2;
use GlobalNamespace\SubSpace\ThirdSpace\Class3;
// reduced to
from GlobalNamespace\SubSpace\ThirdSace use Class1, Class2, Class3 ;
?>


and if we need alias we could：
<?php
// reduce  
use GlobalNamespace\SubSpace\ThirdSpace\Class1;
use GlobalNamespace\SubSpace\ThirdSpace\Class2 as Alias2;
use GlobalNamespace\SubSpace\ThirdSpace\ForthSpace\Class3 as Alias3;
// to
from GlobalNamespace\SubSpace\ThirdSace use Class1, Class2 as Alias2, ForthSpace\Class3 as Alias3;
?>


Thanks stas.
>  
>  
> --  
> Stanislav Malyshev, Software Architect
> SugarCRM: http://www.sugarcrm.com/
> (408)454-6900 ext. 227

---

## #61735 — Stas Malyshev — 2012-07-25T05:22:51+00:00

URL: https://externals.io/message/61691#61735 · In reply to: #61731

Hi!

> No, we can not import namespace directly for now :

Of course you can. You just missing on what namespace import means in
PHP, it's not like Java (though a bit like Python).

> <?php
> namespace A {
>     class B {}
> }
> 
> namespace {
> use A;
> 
> var_dump(new B());
> }

use A is a no-op, just as the warnings tell you. You can just write A\B.
It's only 2 characters more. If your A is longer, then you can alias it
to something shorter.

> and if we need alias we could：
> <?php
> // reduce 
> use GlobalNamespace\SubSpace\ThirdSpace\Class1;
> use GlobalNamespace\SubSpace\ThirdSpace\Class2 as Alias2;
> use GlobalNamespace\SubSpace\ThirdSpace\ForthSpace\Class3 as Alias3;

You could just do
use GlobalNamespace\SubSpace\ThirdSpace as Spc;
$a = new Spc\Class1();


-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227

---

## #61736 — Laruence — 2012-07-25T05:27:04+00:00

URL: https://externals.io/message/61691#61736 · In reply to: #61735

Hi:

is there any really usage?  I didn't see before.  I don't think every
python feature is proper for PHP. PHP is not python.

-1 on this RFC

+1 for Stas

and one more thing, BC break.

there are many ORM or DB warpper libraries defines `from` method, like:

$db->select()->from();

thanks


> Hi!
>
> > […]
>
> Of course you can. You just missing on what namespace import means in
> PHP, it's not like Java (though a bit like Python).
>
> > […]
>
> use A is a no-op, just as the warnings tell you. You can just write A\B.
> It's only 2 characters more. If your A is longer, then you can alias it
> to something shorter.
>
> > […]
>
> You could just do
> use GlobalNamespace\SubSpace\ThirdSpace as Spc;
> $a = new Spc\Class1();
>
>
> --
> Stanislav Malyshev, Software Architect
> SugarCRM: http://www.sugarcrm.com/
> (408)454-6900 ext. 227
>
> --
> 
> 
>



-- 
Laruence  Xinchen Hui
http://www.laruence.com/

---

## #61737 — Reeze Xia — 2012-07-25T06:43:58+00:00

URL: https://externals.io/message/61691#61737 · In reply to: #61736

Hi,  

在 2012年7月25日星期三，下午1:27，Laruence 写道：

> Hi:
>  
> is there any really usage? I didn't see before. I don't think every
Yes, eg: https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/EntityRepository.php#L24

it could be:
from Doctrine\Common\Collections use ExpressionBuilder, Criteria, ArrayCollection, ExpressionBuilder;

The Motivation is that I found many projects that use namespace have the problem of repeat type
the same namespace again and again.  
> python feature is proper for PHP. PHP is not python.
>  
>  

I think it could be useful to reduce duplication, but not copy everything from Python.
like many useful features we borrowed from other languages ;-)
>  
> -1 on this RFC
>  
> +1 for Stas
>  
> and one more thing, BC break.
Yes, introduce new features may break things, eg the new trait,insteadof in 5.4, and maybe  
the comming finally, yield etc. but if it is desired, I think it may worth it.  

Thanks for your feedback!
>  
> there are many ORM or DB warpper libraries defines `from` method, like:
>  
> $db->select()->from();
>  
> thanks
>  
> 
> > […]
>  
>  
>  

Hi Stas,  
That is what I am trying to propose, maybe it could be more DRY?

thanks :)
> > […]
>  
>  
>  
>  
> --  
> Laruence Xinchen Hui
> http://www.laruence.com/

---

## #61738 — Leigh — 2012-07-25T06:54:18+00:00

URL: https://externals.io/message/61691#61738 · In reply to: #61737

What about expanding the syntax instead of introducing new reserved words.

use A\B {
    Class1,
    Class2
}

---

## #61739 — Laruence — 2012-07-25T06:54:35+00:00

URL: https://externals.io/message/61691#61739 · In reply to: #61737

> Hi,
>
> 在 2012年7月25日星期三，下午1:27，Laruence 写道：
>
> Hi:
>
> is there any really usage? I didn't see before. I don't think every
>
> Yes, eg:
> https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/EntityRepository.php#L24
>
> it could be:
> from Doctrine\Common\Collections use ExpressionBuilder, Criteria,
> ArrayCollection, ExpressionBuilder;
>
> The Motivation is that I found many projects that use namespace have the
> problem of repeat type
> the same namespace again and again.
>
> python feature is proper for PHP. PHP is not python.
>
> I think it could be useful to reduce duplication, but not copy everything
> from Python.
> like many useful features we borrowed from other languages ;-)
>
>
> -1 on this RFC
>
> +1 for Stas
>
> and one more thing, BC break.
>
> Yes, introduce new features may break things, eg the new trait,insteadof in
> 5.4, and maybe
> the comming finally, yield etc. but if it is desired, I think it may worth
> it.
yeah,  I think it's not wroth.

and  the key word of mine is "there are many",  but I didn't make it
as a main argument.

thanks
>
> Thanks for your feedback!
>
>
> there are many ORM or DB warpper libraries defines `from` method, like:
>
> $db->select()->from();
>
> thanks
>
> On Wed, Jul 25, 2012 at 1:22 PM, Stas Malyshev <smalyshev@sugarcrm.com>
> wrote:
>
> Hi!
>
> No, we can not import namespace directly for now :
>
>
> Of course you can. You just missing on what namespace import means in
> PHP, it's not like Java (though a bit like Python).
>
> <?php
> namespace A {
> class B {}
> }
>
> namespace {
> use A;
>
> var_dump(new B());
> }
>
>
> use A is a no-op, just as the warnings tell you. You can just write A\B.
> It's only 2 characters more. If your A is longer, then you can alias it
> to something shorter.
>
> and if we need alias we could：
> <?php
> // reduce
> use GlobalNamespace\SubSpace\ThirdSpace\Class1;
> use GlobalNamespace\SubSpace\ThirdSpace\Class2 as Alias2;
> use GlobalNamespace\SubSpace\ThirdSpace\ForthSpace\Class3 as Alias3;
>
>
> You could just do
> use GlobalNamespace\SubSpace\ThirdSpace as Spc;
> $a = new Spc\Class1();
>
> Hi Stas,
> That is what I am trying to propose, maybe it could be more DRY?
>
> thanks :)
>
>
>
> --
> Stanislav Malyshev, Software Architect
> SugarCRM: http://www.sugarcrm.com/
> (408)454-6900 ext. 227
>
> --
> 
> 
>
>
>
>
> --
> Laruence Xinchen Hui
> http://www.laruence.com/
>
>



-- 
Laruence  Xinchen Hui
http://www.laruence.com/

---

## #61740 — Laruence — 2012-07-25T06:58:12+00:00

URL: https://externals.io/message/61691#61740 · In reply to: #61738

> What about expanding the syntax instead of introducing new reserved words.
>
> use A\B {
>     Class1,
>     Class2
> }
better than from :). and a little consistent with use traits in class

thanks

-- 
Laruence  Xinchen Hui
http://www.laruence.com/
