# FW: Simple Namespace Proposal

- Thread: https://externals.io/message/30683
- Original archive: https://news-web.php.net/php.internals/30683
- Started by Dmitry Stogov on 2007-07-09T16:36:43+00:00
- Messages: 5
- 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

---

## #30683 — Dmitry Stogov — 2007-07-09T16:36:43+00:00

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

I am going to commit this patch on Wednesday.

Thanks. Dmitry.

> -----Original Message-----
> From: Dmitry Stogov [mailto:dmitry@zend.com] 
> Sent: Wednesday, July 04, 2007 4:46 PM
> To: 'internals@lists.php.net'
> Subject: Simple Namespace Proposal
> 
> 
> Hi,
> 
> Please review the following concept and patch for php6...
> 
> http://dev.daylessday.org/diff/ns-06.diff.txt
> http://dev.daylessday.org/diff/tests.tar.gz
> 
> Main assumption of the model is that the problem that we are 
> to solve is the problem of the very long class names in PHP 
> libraries. We would not attempt to take autoloader's job or 
> create packaging model - only make names manageable. 
> 
> Namespaces are defined the following way: 
> 
> Zend/DB/Connection.php: 
> <?php 
> namespace Zend::DB;
> 
> class Connection {
> }
> 
> function connect() {
> }
> ?>
> 
> Namespace definition does the following: 
> All class and function names inside are automatically 
> prefixed with namespace name. Inside namespace, local name 
> always takes precedence over global name. It is possible to 
> use the same namespace in several PHP files. The namespace 
> declaration statement must be the very first statement in file.
> 
> Every class and function from namespace can be referred to by 
> the full name - e.g. Zend::DB::Connection or 
> Zend::DB::connect - at any time. 
> 
> <?php
> require 'Zend/Db/Connection.php';
> $x = new Zend::DB::Connection;
> Zend::DB::connect();
> ?>
> 
> Namespace or class name can be imported: 
> 
> <?php 
> require 'Zend/Db/Connection.php';
> import Zend::DB;
> import Zend::DB::Connection as DbConnection;
> $x = new Zend::DB::Connection();
> $y = new DB::connection();
> $z = new DbConnection();
> DB::connect();
> ?>
> 
> import statement only defines name aliasing. It may create 
> name alias for namespace or class. The simple form of 
> statement "import A::B::C::D;" is equivalent to "import 
> A::B::C::D as D;". Import statement can be used at any time 
> in global scope (not inside function/class) and takes effect 
> from the point of definition down to the end of file. It is 
> recommended however to place imports at the beginning of the 
> file. Import statements have effect only on file where they 
> are written.
> 
> The special "empty" namespace (:: prefix) is useful as 
> explicit global namespace qualification. All class and 
> function names started from :: interpreted as global. <?php 
> namespace A::B::C;
> 
> $con = ::mysql_connect(...);
> ?>
> 
> A special constant __NAMESPACE__ indicates the current 
> namespace. It can be used to construct fully-qualified names 
> to pass them as callbacks.
> 
> <?php
> namespace A::B::C;
> 
> function foo() {
> }
> 
> set_error_handler(__NAMESPACE__ . "::foo");
> ?>
> 
> In global namespace __NAMESPACE__ constant has value of empty string.
> 
> Names inside namespace are resolved according to the following rules.
> 
> 1) all qualified names are translated during compilation 
> according to current import rules. So if we have "import 
> A::B::C;" and then "C::D::e();" it is translated to "A::B::C::D::e()"
> 2) unqualified class names translated during compilation 
> according to current import rules. So if we have "import 
> A::B::C;" and then "new C();" it is translated to "new A::B::C()"
> 
> 3) calls to unqualified functions that are defined in current 
> namespace interpreted as calls to corresponding functions
> 4) calls to unqualified functions that are not defined in 
> current namespace are resolved in run-time. The call to 
> function foo() inside namespace (A::B) first tries to find 
> and call function from current namespace A::B::foo() and if 
> it doesn't exist PHP tries to call internal function foo(). 
> Note that using foo() in namespace you can call only internal 
> PHP functions, however using ::foo() you are able to call any 
> function from global namespace.
> 5) unqualified class names are resolved at run-time. E.q. 
> "new Exception()" first tries to use (end even __autoload()) 
> class from current namespace and in case of failure uses 
> internal PHP class. Note that using "new A" in namespace you 
> can call only create internal PHP class, however using "new 
> ::A" you are able to create any class from global namespace
> 6) Calls to qualified functions are resolved at run-time. 
> Call to "A::B::foo()" first tries to call function foo() from 
> namespace "A::B", then it tries to find class "A::B 
> (__autoload() it if necessary) and call its static function foo() 
> 7) qualified class names are interpreted as class from 
> corresponding namespace. So "new A::B::C()" creates class "C" 
> from namespace "A::B".
> 
> Thanks. Dmitry.

---

## #30901 — Markus Fischer — 2007-07-14T06:30:23+00:00

URL: https://externals.io/message/30683#30901 · In reply to: #30683

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

as only being a general lurker on this list, I remmeber when Namespaces
came up the first time, there were some technical problems (or just
syntactical sugar ones?) with the "::" operator also being used as a
namespace separate additional being a class separator.

How has this been handled or are there some caveats to watch for?

thank you,
- - Markus
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGmG1/1nS0RcInK9ARAlMBAJ9ixpfVZ4GZQF8u0Lgd+HJall9pLgCgo9gA
ogZkPQ+mgJcDxESEYGizkYs=
=9OAV
-----END PGP SIGNATURE

---

## #30902 — Markus Fischer — 2007-07-14T06:31:06+00:00

URL: https://externals.io/message/30683#30902 · In reply to: #30683

Hi,

as only being a general lurker on this list, I remember when Namespaces
came up the first time, there were some technical problems (or just
syntactical sugar ones?) with the "::" operator also being used as a
Namespace separate additional being a class separator.

How has this been handled or are there some caveats to watch for?

thank you,
- Markus

---

## #30908 — Stanislav Malyshev — 2007-07-14T06:59:33+00:00

URL: https://externals.io/message/30683#30908 · In reply to: #30902

> as only being a general lurker on this list, I remember when Namespaces
> came up the first time, there were some technical problems (or just
> syntactical sugar ones?) with the "::" operator also being used as a
> Namespace separate additional being a class separator.
> 
> How has this been handled or are there some caveats to watch for?

There's still some ambiguity in cases like Foo::bar() but since we 
explicitly import namespace shortcut names we always know if there's an 
import name that can be substituted or class name. This means some 
slowdown in "hard" cases since we have to first check for import and 
then for class name but it's compile-time.
-- 
Stanislav Malyshev, Zend Software Architect
stas@zend.com   http://www.zend.com/
(408)253-8829   MSN: stas@zend.com

---

## #30989 — Markus Fischer — 2007-07-16T18:52:04+00:00

URL: https://externals.io/message/30683#30989 · In reply to: #30901

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I'm eager to try this new feature our but I'm dependent on the win32
built from snaps which unfortunately currently doesn't build (the last
build was before the namespace commits).

It seems the other platforms currently don't build either.

thanks
- - Markus

Markus Fischer wrote:
> Hi,
> 
> as only being a general lurker on this list, I remmeber when Namespaces
> came up the first time, there were some technical problems (or just
> syntactical sugar ones?) with the "::" operator also being used as a
> namespace separate additional being a class separator.
> 
> How has this been handled or are there some caveats to watch for?
> 
> thank you,
> - Markus
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGm75U1nS0RcInK9ARAkn1AJ9VLFG6jfG99ZS7IywSYq85CZHV3ACgrJnh
xK+jmqw9NHdsrNm1AmiXXo4=
=ZtSv
-----END PGP SIGNATURE
