AW: [STANDARDS] Use declaration outside of namespace
[email protected] ("Robert Stoll")
| Newsgroups | php.standards |
|---|---|
| Message-ID | <[email protected]> |
> -----Ursprüngliche Nachricht----- > Von: Ben Ramsey [mailto:[email protected]] > Gesendet: Mittwoch, 29. Oktober 2014 15:20 > An: Chris Wright > Cc: Robert Stoll; [email protected] > Betreff: Re: [STANDARDS] Use declaration outside of namespace > > > > On Oct 29, 2014, at 9:04 AM, Chris Wright <[email protected]> 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"