AW: AW: [PHP-DEV] forbid use declaration outside of a namespace in PHP 7
| From: | Robert Stoll | Date: | Thu, 13 Nov 2014 12:30:49 +0000 |
| Subject: | AW: AW: [PHP-DEV] forbid use declaration outside of a namespace in PHP 7 | ||
| References: | 1 2 3 4 5 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-78893@lists.php.net to get a copy of this message | ||
> -----Ursprüngliche Nachricht-----
> Von: Rowan Collins [mailto:rowan.collins@gmail.com]
> Gesendet: Donnerstag, 13. November 2014 11:57
> An: internals@lists.php.net
> Betreff: Re: AW: [PHP-DEV] forbid use declaration outside of a namespace in PHP 7
>
> Robert Stoll wrote on 12/11/2014 21:27:
>
> > That's still perfectly fine because in your code the use statement is not outside of
> > a namespace, it is implicitly in the
> default namespace.
>
> Surely if that's true of Adam's example, it's true of yours as well?
[Robert Stoll]
The difference is that I have specified a namespace and thus code outside of that namespace is no
longer implicitly in the default namespace.
> > namespace a{
> > }
> >
> > use some\UseDeclaration\which\is\outside\of\AnyNamespace as Useless;
> > // sorry for begin biased ^^
> >
> > namespace test{
> > }
>
> This scenario looks identical to Adam's to me, unless you propose to look at the entirety
> of the file to detect if there is
> something other than a use statement in the default namespace.
[Robert Stoll]
Same applies here, my use statement is outside of a namespace Adam's isn't.
> For that matter, what about this:
>
> use GuzzleHttp\Client;
>
> include __DIR__.'/vendor/autoload.php';
>
> $client = new Client;
> // $client is a GuzzleHttp\Client object
>
> namespace Foo;
> $client = new Client;
> // $client is a Foo\Client object
>
> Certainly this code is not *sensible*, but it does have a meaningful behaviour, which derives
> directly from the previous
> examples and documented uses of the namespace keyword. I'm not sure how you'd
> distinguish between the various
> combinations here.
>
> Regards,
> --
> Rowan Collins
> [IMSoP]
>
[Robert Stoll]
I haven't tested it I am pretty sure this script would fail since $client = new Client is
outside of a namespace - an error message something like "namespace must be the first
statement" would show up.
If I rewrite your code to the following:
namespace{
use GuzzleHttp\Client;
include __DIR__.'/vendor/autoload.php';
$client = new Client;
// $client is a GuzzleHttp\Client object
namespace Foo{
$client = new Client;
// $client is a Foo\Client object
}
Then it would make sense (use statement is inside a namespace).