multiple namespace per file

php.internals

Stanislav Malyshev

18 years ago
Hi! Following the feedback from the community, we (mostly me and Dmitry) tried to find a good model that would allow multiple namespaces per file without running into too many problems and complications, and would allow to bundle multiple namespaced files together without modifications. It looks like it is possible to do, but only under the following condition: the file can have multiple namespaces, but if the file has namespaces, then it can have no code outside namespaces. For example, this would work: namespace A; class X {} namespace B; class Y {} This would not work: class X {} namespace A; class Y {} This would not work either: require 'foo/bar.php'; namespace A; class X {} This however would work: namespace A; require 'foo/bar.php'; class X {} namespace B; class Y {} So, for the people that wanted multiple NS per file, would such solution work? P.S. this is *not* a "should we use braces" thread, so please don't :)
-- Stanislav Malyshev, Zend Software Architect stas@zend.com http://www.zend.com/ (408)253-8829 MSN: stas@zend.com

David Coallier

18 years ago
On 9/10/07, Stanislav Malyshev <stas@zend.com> wrote:
> Hi! > > Following the feedback from the community, we (mostly me and Dmitry) > tried to find a good model that would allow multiple namespaces per file > without running into too many problems and complications, and would > allow to bundle multiple namespaced files together without > modifications. It looks like it is possible to do, but only under the > following condition: the file can have multiple namespaces, but if the > file has namespaces, then it can have no code outside namespaces. > > For example, this would work: > namespace A; > class X {} > namespace B; > class Y {} > > This would not work: > class X {} > namespace A; > class Y {} > > This would not work either: > require 'foo/bar.php'; > namespace A; > class X {} > > This however would work: > namespace A; > require 'foo/bar.php'; > class X {} > namespace B; > class Y {} > > So, for the people that wanted multiple NS per file, would such solution > work? > > P.S. this is *not* a "should we use braces" thread, so please don't :)
Good call :)
> -- > Stanislav Malyshev, Zend Software Architect > stas@zend.com http://www.zend.com/ > (408)253-8829 MSN: stas@zend.com > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > >
Ok, I was wondering to myself, do you guys think it would be possible to do something like having an endnamespaces; keyword that would put an end to the namespace definition? For instance: <?php namespace A; class AX{} class AY{} namespace B; class BX{} endnamespaces; class BAX{} class Extern {} ?> I am trying to find a way to put a delimiter to the namespaces definitions.. I'm ready and open for a good and constructive discussion :) No more hate.
-- David Coallier, Founder & Software Architect, Agora Production (http://agoraproduction.com) 51.42.06.70.18

Stanislav Malyshev

18 years ago
> Ok, I was wondering to myself, do you guys think it would be possible > to do something like having an endnamespaces; keyword that would put > an end to the namespace definition?
Oh no :) It doesn't look good and it doesn't solve the problem. Hint: the braces have the same problem (that's why I asked not to suggest them :) since the problem is per-file imports vs. multiple global spaces. I can explain more if you wish but basically it's not about finding the right delimiter, it's about making something that would work when files are combined together (repeatedly). If you allow global space there, it starts to look and work seriously weird.
-- Stanislav Malyshev, Zend Software Architect stas@zend.com http://www.zend.com/ (408)253-8829 MSN: stas@zend.com

David Coallier

18 years ago
On 9/10/07, Stanislav Malyshev <stas@zend.com> wrote:
> > Ok, I was wondering to myself, do you guys think it would be possible > > to do something like having an endnamespaces; keyword that would put > > an end to the namespace definition? > > Oh no :) It doesn't look good and it doesn't solve the problem. Hint: > the braces have the same problem (that's why I asked not to suggest them > :) since the problem is per-file imports vs. multiple global spaces. I > can explain more if you wish but basically it's not about finding the > right delimiter, it's about making something that would work when files > are combined together (repeatedly). If you allow global space there, it > starts to look and work seriously weird. > --
Yeah you are right, I also thought it looked extremely ugly. I have thought of a few things (since I am the combined/aggregated files advocate) and I actually think that this approach (Having multiple namespace per file like you mentionned) would work pretty well. After taking a look at this problem from another point of view, I realized that if we can manage to make the multiple namespace per file as you mentionned earlier then it would be rather trivial. I like the implementation you suggested earlier, you got +1 from me ;-) More question, just like this, when you do: <?php namespace A; require 'somefile.php'; class AX{} namespace B; class BX{ } ?> is somefile.php also included in the B namespace since you cannot include it in global scope. Next step ? Renaming to packages ? :P (No please dont anyone it's just a joke)
> Stanislav Malyshev, Zend Software Architect > stas@zend.com http://www.zend.com/ > (408)253-8829 MSN: stas@zend.com > >
Thanks,
-- David Coallier, Founder & Software Architect, Agora Production (http://agoraproduction.com) 51.42.06.70.18

Stanislav Malyshev

18 years ago
> <?php > > namespace A; > require 'somefile.php'; > class AX{} > > namespace B; > class BX{ } > > ?> > > is somefile.php also included in the B namespace since you cannot > include it in global scope.
No, somefile.php does not automatically belong to either A or B, it can contain namespaces definition inside and thus belong to either A or B or neither, but it is parsed as entirely separate entity. I general once the file is done, everything is forgotten (what happens in the file, stays in the file ;). Also remember require is run-time so it can't influence namespaces since those are compile-time (in other words, when we parse namespaces we don't know what's in somefile.php, when we parse somefile.php we don't know namespaces in including file existed).
-- Stanislav Malyshev, Zend Software Architect stas@zend.com http://www.zend.com/ (408)253-8829 MSN: stas@zend.com

Marcus Börger

18 years ago
Hello Stanislav, Tuesday, September 11, 2007, 1:21:07 AM, you wrote:
> Hi!
> Following the feedback from the community, we (mostly me and Dmitry) > tried to find a good model that would allow multiple namespaces per file > without running into too many problems and complications, and would > allow to bundle multiple namespaced files together without > modifications. It looks like it is possible to do, but only under the > following condition: the file can have multiple namespaces, but if the > file has namespaces, then it can have no code outside namespaces.
> For example, this would work: > namespace A; > class X {} > namespace B; > class Y {}
This looks very messy. We should either go with curly braces or with one namespace at the top of a file. And even if we do multiple namespaces per file. I guess we do not allow a naespace to be spread onto several files, right? The current implementation is not clear at all. Since everything following a namespace declaration belongs into that declaration and since nothing is allowed ontop of that declaration I'd expect that the second namespace would actually create an inner naespace. Either way dealing with this without curly braces requires an IDE while today I still edit nearly all of my PHP with a simple text editor. And even when assuming everyone would use an IDE, the implementation of multiple braceless namespace is very tricky and more important hard to visualize and thus confusing for the user. marcus
> This would not work: > class X {} > namespace A; > class Y {}
> This would not work either: > require 'foo/bar.php'; > namespace A; > class X {}
> This however would work: > namespace A; > require 'foo/bar.php'; > class X {} > namespace B; > class Y {}
> So, for the people that wanted multiple NS per file, would such solution > work?
> P.S. this is *not* a "should we use braces" thread, so please don't :) > -- > Stanislav Malyshev, Zend Software Architect > stas@zend.com http://www.zend.com/ > (408)253-8829 MSN: stas@zend.com
Best regards, Marcus

Dmitry Stogov

18 years ago
> And even if we do > multiple namespaces per file. I guess we do not allow a > namespace to be spread onto several files, right?
Current implementation allows to spread one namespace on several files. You should just use "namespace" with the same name on top of each file. (Very similar to Java) This won't be changed even if we will use several namespaces per file. Thanks. Dmitry.

Larry Garfield

18 years ago
On Tuesday 11 September 2007, Marcus Boerger wrote:
> Hello Stanislav, > > Tuesday, September 11, 2007, 1:21:07 AM, you wrote: > > Hi! > > > > Following the feedback from the community, we (mostly me and Dmitry) > > tried to find a good model that would allow multiple namespaces per file > > without running into too many problems and complications, and would > > allow to bundle multiple namespaced files together without > > modifications. It looks like it is possible to do, but only under the > > following condition: the file can have multiple namespaces, but if the > > file has namespaces, then it can have no code outside namespaces. > > > > For example, this would work: > > namespace A; > > class X {} > > namespace B; > > class Y {} > > This looks very messy. We should either go with curly braces or with one > namespace at the top of a file. And even if we do multiple namespaces per > file. I guess we do not allow a naespace to be spread onto several files, > right?
From a readability-usability perspective (which is all I am able to offer), I'd have to agree with Marcus. The syntax should follow the structure of the parser. So either: <?php // One namespace per file only namespace Foo; class X {} function bar() {} ?> or <?php // Multiple namespaces permitted namespace Foo { class X {} } namespace Baz { function bar() {} } I agree that allowing stuff in a file outside of a namespace if namespaces are used would be all kinds of confusing with either syntax.
-- Larry Garfield AIM: LOLG42 larry@garfieldtech.com ICQ: 6817012 "If nature has made any one thing less susceptible than all others of exclusive property, it is the action of the thinking power called an idea, which an individual may exclusively possess as long as he keeps it to himself; but the moment it is divulged, it forces itself into the possession of every one, and the receiver cannot dispossess himself of it." -- Thomas Jefferson

David Coallier

18 years ago
On 9/11/07, Larry Garfield <larry@garfieldtech.com> wrote:
> On Tuesday 11 September 2007, Marcus Boerger wrote: > > Hello Stanislav, > > > > Tuesday, September 11, 2007, 1:21:07 AM, you wrote: > > > Hi! > > > > > > Following the feedback from the community, we (mostly me and Dmitry) > > > tried to find a good model that would allow multiple namespaces per file > > > without running into too many problems and complications, and would > > > allow to bundle multiple namespaced files together without > > > modifications. It looks like it is possible to do, but only under the > > > following condition: the file can have multiple namespaces, but if the > > > file has namespaces, then it can have no code outside namespaces. > > > > > > For example, this would work: > > > namespace A; > > > class X {} > > > namespace B; > > > class Y {} > > > > This looks very messy. We should either go with curly braces or with one > > namespace at the top of a file. And even if we do multiple namespaces per > > file. I guess we do not allow a naespace to be spread onto several files, > > right? > > From a readability-usability perspective (which is all I am able to offer), > I'd have to agree with Marcus. The syntax should follow the structure of the > parser. So either: > > <?php // One namespace per file only > namespace Foo; > class X {} > function bar() {} > ?> > > or > > <?php // Multiple namespaces permitted > namespace Foo { > class X {} > } > > namespace Baz { > function bar() {} > } > > I agree that allowing stuff in a file outside of a namespace if namespaces are > used would be all kinds of confusing with either syntax. > > -- > Larry Garfield AIM: LOLG42 > larry@garfieldtech.com ICQ: 6817012 > > "If nature has made any one thing less susceptible than all others of > exclusive property, it is the action of the thinking power called an idea, > which an individual may exclusively possess as long as he keeps it to > himself; but the moment it is divulged, it forces itself into the possession > of every one, and the receiver cannot dispossess himself of it." -- Thomas > Jefferson > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > >
Most of us do agree, but Stas specifically specified that this is not a { } thread :)
-- David Coallier, Founder & Software Architect, Agora Production (http://agoraproduction.com) 51.42.06.70.18

Lukas Kahwe Smith

18 years ago
Stanislav Malyshev wrote:
> So, for the people that wanted multiple NS per file, would such solution > work?
I think the limitation is acceptable.
> P.S. this is *not* a "should we use braces" thread, so please don't :)
The syntax is not, but since you disallow discussing this in this thread, I will refrain from mentioning curly braces (ooops). regards, Lukas

Greg Beaver

18 years ago
Stanislav Malyshev wrote:
> Hi! > > Following the feedback from the community, we (mostly me and Dmitry) > tried to find a good model that would allow multiple namespaces per file > without running into too many problems and complications, and would > allow to bundle multiple namespaced files together without > modifications. It looks like it is possible to do, but only under the > following condition: the file can have multiple namespaces, but if the > file has namespaces, then it can have no code outside namespaces. > > For example, this would work: > namespace A; > class X {} > namespace B; > class Y {} > > This would not work: > class X {} > namespace A; > class Y {} > > This would not work either: > require 'foo/bar.php'; > namespace A; > class X {} > > This however would work: > namespace A; > require 'foo/bar.php'; > class X {} > namespace B; > class Y {} > > So, for the people that wanted multiple NS per file, would such solution > work?
Hi, This explanation could be slightly clearer. The most important thing to realize is that allowing multiple namespaces per file is not an attempt to allow a development paradigm where multiple namespaces exist in the same file. Rather, the goal is to allow multiple *files* to be combined into a single file. A benchmark devised by David and modified by me proves that the performance difference between separate files and a single file containing the same stuff is at a minimum 10%, and can be up to 30% difference even with APC and apc.no_stat=1 Allowing multiple namespaces per file should instead be thought of as "allowing several files to be cut/pasted into a single file" and not as a new way to develop in PHP. The proposed syntax is in fact somewhat difficult to read, which discourages development along these lines, but it does make it possible to combine files in a performance-hungry setting. Once the syntax is thought of along these lines, it makes a lot more sense. It's basically one-namespace-per-file but PHP allows you to virtually combine files so that at each namespace declaration, the imports are reset. Greg

Marc Gear

18 years ago
On 9/11/07, Stanislav Malyshev <stas@zend.com> wrote:
> if the > file has namespaces, then it can have no code outside namespaces.
This seems like a great compromise, FWIW +1 here. Does closing php tags end the namespace declaration? ie would the following work: namespace foo; class a { public function bar() { ?>somerawoutput<?php } namespace foobar; class someclass{} ??
-- Marc Gear marcgear@gmail.com

Stanislav Malyshev

18 years ago
> Does closing php tags end the namespace declaration? ie would the > following work:
php tags would not influence namespace declarations, i.e. it would continue as if there were just PHP code.
-- Stanislav Malyshev, Zend Software Architect stas@zend.com http://www.zend.com/ (408)253-8829 MSN: stas@zend.com