# Simple Namespace Proposal

- Thread: https://externals.io/message/30440
- Original archive: https://news-web.php.net/php.internals/30440
- Started by Dmitry Stogov on 2007-07-04T12:46:06+00:00
- Messages: 7
- 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

---

## #30440 — Dmitry Stogov — 2007-07-04T12:46:06+00:00

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

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.

---

## #30445 — Brian Moon — 2007-07-04T14:33:13+00:00

URL: https://externals.io/message/30440#30445 · In reply to: #30440

> It is possible to use the same namespace in several PHP files.

Does this mean that a project like Phorum could declare the same 
namespace at the top of all of our files and all the code in those files 
would exist in the same namespace?

common.php:
<?php
namespace Phorum;

$PHORUM = get_settings();

function phorum_build_url(){
     return $url;
}
?>

list.php:
<?php
namespace Phorum;

$url - phorum_build_url();

?>


Or, would list.php have to import the Phorum namespace?

Also, I don't see any mention of variables in namespaces.  Will there be 
no effect on variables?

-- 

Brian Moon
Senior Developer
------------------------------
http://dealnews.com/
It's good to be cheap =)

---

## #30446 — Stefan Priebsch — 2007-07-04T14:53:50+00:00

URL: https://externals.io/message/30440#30446 · In reply to: #30440

Hi Dmitry,

it would be great to have namespaces in PHP6, as these always
longer-getting classnames are somewhat annoying.

I don't want to start a discussion about the separator (I think there
was one before), but:

> > […]
"A::B", then
> > […]

If I am not mislead this ambiguity is due to the fact that :: is the
namespace separator? If, so I'd rather suggest changing the separator,
because one could alter the program's behaviour by adding a function
foo() to a namespace thus making PHP call this function instead of the
method. Besides, a lot of less experiences programmers would not be sure
when which function/method is actually called.


> > […]
over
> > […]
files.
> > […]

I take it that when I include or conditionally include, the included
file has its own namespace, if one is declared in that file. What if I
do not declare a namespace in an include file? Does this file "inherit"
the current namespace or are the contents added to the global namespace?

> > […]
however to
> > […]
effect
> > […]

Could this result in name conflicts when including files that have their
own import statements?


> > […]
namespace
> > […]
(A::B)
> > […]
A::B::foo() and
> > […]
however
> > […]

Does this mean that a programmer could "override" an internal PHP
function, possibly accidentally?


Kind regards,

Stefan


-- 
 >e-novative> - We make IT work for you.

 e-novative GmbH - HR: Amtsgericht München HRB 139407
 Sitz: Wolfratshausen - GF: Dipl. Inform. Stefan Priebsch

 http://www.e-novative.de

---

## #30449 — Stanislav Malyshev — 2007-07-04T19:14:17+00:00

URL: https://externals.io/message/30440#30449 · In reply to: #30445

> Does this mean that a project like Phorum could declare the same 
> namespace at the top of all of our files and all the code in those files 
> would exist in the same namespace?

Theoretically, yes, that's the idea. Practically there could be some 
problems with variable names - right now the patch does not resolve 
names, so if you do $name = "func", $name("foo"); it might not work, 
you'd need to use __NAMESPACE__ currently. I'm not sure how much of a 
trouble it is.

> Or, would list.php have to import the Phorum namespace?

no, no import is necessary for same-namespace references.

> Also, I don't see any mention of variables in namespaces.  Will there be 
> no effect on variables?

No namespace variables, right. For grouping variables you have arrays 
and classes, should be good enough ;)
-- 
Stanislav Malyshev, Zend Software Architect
stas@zend.com   http://www.zend.com/
(408)253-8829   MSN: stas@zend.com

---

## #30450 — Stanislav Malyshev — 2007-07-04T19:18:53+00:00

URL: https://externals.io/message/30440#30450 · In reply to: #30446

> I take it that when I include or conditionally include, the included
> file has its own namespace, if one is declared in that file. What if I
> do not declare a namespace in an include file? Does this file "inherit"
> the current namespace or are the contents added to the global namespace?

No, namespaces are per-file. No namespace means global namespace. 
Non-local namespaces can be very problematic for bytecode caches since 
it would not be possible to know "true" names of the classes per-file 
and actually same file could generate very different names.

> Could this result in name conflicts when including files that have their
> own import statements?

Note that import defines local (per-file) name transformation. I.e. if 
you do import Zend::Some::Class as Myclass, it means in this file, once 
you say Myclass, you actually mean Zend::Some::Class. But it should not 
have any effects on other files.

> Does this mean that a programmer could "override" an internal PHP
> function, possibly accidentally?

Yes, it would be possible to "override" internal functions in namespace. 
I don't think it's too much of a problem - tools can deal with it.
-- 
Stanislav Malyshev, Zend Software Architect
stas@zend.com   http://www.zend.com/
(408)253-8829   MSN: stas@zend.com

---

## #30686 — Derick Rethans — 2007-07-09T18:13:23+00:00

URL: https://externals.io/message/30440#30686 · In reply to: #30440

> The namespace declaration statement must be the very first statement in
> file.

I thought that was reserved in PHP 6 for the "pragma(encoding=UTF-8);" 
statement? Which of the two needs to be first, and which second?

regards,
Derick

-- 
Derick Rethans
http://derickrethans.nl | http://ez.no | http://xdebug.org

---

## #30687 — Andrei Zmievski — 2007-07-09T18:26:53+00:00

URL: https://externals.io/message/30440#30687 · In reply to: #30686

That's why I proposed (privately) to Dmitry to use declare() for  
namespace declaration.

-Andrei




> 
>
> > […]
>
> I thought that was reserved in PHP 6 for the "pragma(encoding=UTF-8);"
> statement? Which of the two needs to be first, and which second?
>
> regards,
> Derick
>
> -- 
> Derick Rethans
> http://derickrethans.nl | http://ez.no | http://xdebug.org
