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.
-
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()" -
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()" -
calls to unqualified functions that are defined in current namespace
interpreted as calls to corresponding functions -
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. -
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 -
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() -
qualified class names are interpreted as class from corresponding
namespace. So "new A::B::C()" creates class "C" from namespace "A::B".
Thanks. Dmitry.
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 =)
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
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:
- 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()
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.
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.
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?
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.
Could this result in name conflicts when including files that have their
own import statements?
- 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.
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
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
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
That's why I proposed (privately) to Dmitry to use declare() for
namespace declaration.
-Andrei
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