# multiple namespace per file

- Thread: https://externals.io/message/32220
- Original archive: https://news-web.php.net/php.internals/32220
- Started by Stanislav Malyshev on 2007-09-10T23:21:07+00:00
- Messages: 12
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #32220 — Stanislav Malyshev — 2007-09-10T23:21:07+00:00

URL: https://externals.io/message/32220#32220

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

---

## #32222 — David Coallier — 2007-09-10T23:28:15+00:00

URL: https://externals.io/message/32220#32222 · In reply to: #32220

> 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
>
> --
> 
> 
>
>

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

---

## #32224 — Stanislav Malyshev — 2007-09-10T23:38:15+00:00

URL: https://externals.io/message/32220#32224 · In reply to: #32222

> 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

---

## #32225 — David Coallier — 2007-09-10T23:58:39+00:00

URL: https://externals.io/message/32220#32225 · In reply to: #32224

> > […]
>
> 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

---

## #32226 — Stanislav Malyshev — 2007-09-11T00:02:43+00:00

URL: https://externals.io/message/32220#32226 · In reply to: #32225

> <?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

---

## #32238 — Marcus Boerger — 2007-09-11T09:34:36+00:00

URL: https://externals.io/message/32220#32238 · In reply to: #32220

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

---

## #32239 — Lukas Kahwe Smith — 2007-09-11T09:39:03+00:00

URL: https://externals.io/message/32220#32239 · In reply to: #32220

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

---

## #32274 — Gregory Beaver — 2007-09-11T21:32:09+00:00

URL: https://externals.io/message/32220#32274 · In reply to: #32220

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

---

## #32277 — Larry Garfield — 2007-09-12T01:40:10+00:00

URL: https://externals.io/message/32220#32277 · In reply to: #32238

> Hello Stanislav,
>
> Tuesday, September 11, 2007, 1:21:07 AM, you wrote:
> > […]
>
> 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

---

## #32278 — David Coallier — 2007-09-12T01:51:08+00:00

URL: https://externals.io/message/32220#32278 · In reply to: #32277

> 
> > […]
>
> 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
>
> --
> 
> 
>
>

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

---

## #32400 — Marc Gear — 2007-09-20T14:30:05+00:00

URL: https://externals.io/message/32220#32400 · In reply to: #32220

> 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

---

## #32403 — Stanislav Malyshev — 2007-09-20T18:17:04+00:00

URL: https://externals.io/message/32220#32403 · In reply to: #32400

> 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
