回复: [PHP-DEV] [RFC] Proposal namespace importing with from syntax

From: Date: Wed, 25 Jul 2012 02:43:33 +0000
Subject: 回复: [PHP-DEV] [RFC] Proposal namespace importing with from syntax
References: 1 2  Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message
Hi Stas,  

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

> 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
>  
>  

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
>  
>  




Thread (11 messages)

« previous php.internals (#61731) next »