AW: [STANDARDS] Use declaration outside of namespace
| From: | Robert Stoll | Date: | Wed, 29 Oct 2014 14:31:07 +0000 |
| Subject: | AW: [STANDARDS] Use declaration outside of namespace | ||
| References: | 1 2 3 4 5 | Groups: | php.standards |
| Request: | Send a blank email to standards-+get-533@lists.php.net to get a copy of this message | ||
> -----Ursprüngliche Nachricht-----
> Von: Ben Ramsey [mailto:ben@benramsey.com]
> Gesendet: Mittwoch, 29. Oktober 2014 15:20
> An: Chris Wright
> Cc: Robert Stoll; standards@lists.php.net
> Betreff: Re: [STANDARDS] Use declaration outside of namespace
>
>
> > On Oct 29, 2014, at 9:04 AM, Chris Wright <cw@daverandom.com> wrote:
> >
> > It does, this example does indeed place something *outside* a
> > namespace, and I am quite surprised that PHP doesn't bork at this
> > already. I would hope no-one would ever write code like this anyway,
> > but I'm sure I would be disappointed.
> >
>
> I write scripts all the time that I run from the command line, and these use namespaces but do
> not specify a namespace
of
> their own. So, these use statements are *outside* a namespace. Are you saying this is incorrect
> to do?
>
> For example:
>
> <?php
>
> require_once 'vendor/autoload.php';
>
> use Foo\Bar;
>
> $bar = new Bar();
> $bar->doSomethingWithBar();
>
> ?>
No not at all, if you do not specify a namespace then your code is placed into the default
namespace. That is perfectly
fine. However, if you define a namespace and then place your code (expect use declarations) really
outside that
namespace as in the following example:
<?php
namespace test{
//inside a namespace
}
//outside a namespace, not even in the default namespace -> PHP will emit an error
require_once 'vendor/autoload.php';
use Foo\Bar;
$bar = new Bar();
$bar->doSomethingWithBar();
?>
then PHP will emit an error (which is the correct behaviour). It should also emit an error for the
use declaration
outside a namespace but this is a different story (should be addressed in PHP 7). I am writing to
the standard list
since I believe the specification should not cover this "feature"