AW: [STANDARDS] Use declaration outside of namespace

[email protected] ("Robert Stoll")
Newsgroups php.standards
Message-ID <[email protected]>
> -----Ursprüngliche Nachricht-----
> Von: [email protected] [mailto:[email protected]] Im Auftrag von Chris Wright
> Gesendet: Mittwoch, 29. Oktober 2014 13:38
> An: Robert Stoll
> Cc: [email protected]
> Betreff: Re: [STANDARDS] Use declaration outside of namespace
> 
> On 29 October 2014 09:12, Robert Stoll <[email protected]> wrote:
> 
> > Heya,
> >
> > I always found it very ugly that it is possible to define a use
> > outside of a namespace. Consider the following:
> >
> > use foo\Bar;
> > namespace test;
> > new Bar(); //error, test\Barnot found
> >
> > I do not see why we should actually support this behaviour in the
> > specification of PHP. I do not see it as a bug in the current PHP
> > implementation but I think we should consider to remove it in PHP 7.
> > Although, it is not a bug, the use declaration is simply ignored as
> > far as I can tell, but I suppose it confuses the user. Nevertheless,
> > even if we declare it as a "feature" I think it should not be a
> > "feature" of the specification.
> >
> > Thoughts?
> >
> >
> TL;DR if you revise wording of the suggested change from "outside of a namespace" to "before a namespace declaration",
> +1
> 
> Removing the ability "use" things outside a namespace is not acceptable to me. I have often written quick scripts that use
> things from many namespaces but don't belong in a specific namespace, and in those cases I usually don't declare a
> namespace (it's also worth noting that you're never "outside a namespace", you're just in the root namespace by default).
> 
> That said, placing the "use" statements before the namespace declaration doesn't make a whole bunch of sense and
> should probably be explicitly disallowed. The fact that it works in php-src at the moment is arguably an oversight, if you try
> to actually *do* anything before the namespace declaration you even get an error stating that it "has to be the very first
> statement in the script" - so this case should probably be disallowed.
> 
> Thanks, Chris
> 
> 
> > Cheers,
> > Robert
> >
> >

To  be clear on this. I am not speaking about statements "outside" of a namespace (which is not possible as you mentioned as well). I am speaking about "use declarations" outside of a namespace and with outside I mean actually outside.

As you mentioned, when one tries to do the following

$a = 1;
namespace test;

This will fail because $a is outside of a namespace (you will get the error message "Namespace declaration statement has to be the very first statement in the script")
Maybe it was not clear enough, so I am going to rewrite my example to be clearer:

namespace{ //default namespace
  $a = 1;
}
use foo\Bar;
namespace test{
  new Bar();
}

I hope that clears things up.

Cheers,
Robert
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.