Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
some rewording and typofixing
  • Loading branch information
smalyshev committed Aug 17, 2007
1 parent 3773079 commit d92a0aa
Showing 1 changed file with 43 additions and 45 deletions.
88 changes: 43 additions & 45 deletions README.namespaces
Expand Up @@ -22,11 +22,11 @@ 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. Several files may be included into the same namespace.
global name. Several files may be using the same namespace.
The namespace declaration statement must be the very first statement in
file. The only exception is "declare" statement that can be used before.
the file. The only exception is "declare" statement that can be used before.

Every class and function from namespace can be referred to by the full name
Every class and function in a namespace can be referred to by the full name
- e.g. Zend::DB::Connection or Zend::DB::connect - at any time.

<?php
Expand All @@ -41,6 +41,7 @@ Namespace or class name can be imported:
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();
Expand All @@ -50,20 +51,23 @@ 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
time in the 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.
only on the file where they appear.

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;
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.
A special constant __NAMESPACE__ contains the name of the current namespace.
It can be used to construct fully-qualified names to pass them as callbacks.

<?php
namespace A::B::C;
Expand All @@ -74,37 +78,37 @@ function foo() {
set_error_handler(__NAMESPACE__ . "::foo");
?>

In global namespace __NAMESPACE__ constant has value of empty string.
In global namespace __NAMESPACE__ constant has the value of empty string.

Names inside namespace are resolved according to the following rules.
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()"
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.
current import rules. So if we have "import A::B::C" and then "new C()" it
is translated to "new A::B::C()".
3) inside namespace, calls to unqualified functions that are defined in
current namespace (and are known at the time the call is parsed) are
interpreted as calls to these namespace functions.
4) inside namespace, calls to unqualified functions that are not defined
in current namespace are resolved at 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() inside namespace you can call only
internal PHP functions, however using ::foo() you are able to call any
function from the 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 class from this namespace or
internal PHP class, however using "new ::A" you are able to create any class
from global namespace
first tries to use (and autoload) class from current namespace and in case
of failure uses internal PHP class. Note that using "new A" in namespace
you can only create class from this namespace or internal PHP class, however
using "new ::A" you are able to create any class from the 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
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 method foo()
7) qualified class names are interpreted as class from corresponding
namespace. So "new A::B::C()" creates class "C" from namespace "A::B".
namespace. So "new A::B::C()" refers to class C from namespace A::B.

Examples
--------
Expand All @@ -123,7 +127,7 @@ new ::B(); // creates object of class "B" defined in global scope
?>

<?php
namespcae A;
namespace A;
new A(); // first tries to create object of class "A" from namespace "A" (A::A)
// then creates object of internal class "A"
?>
Expand All @@ -150,27 +154,21 @@ A::foo(); // first tries to call function "foo" from namespace "A::A"
TODO
====

* Rename namespaces to packages?

* Support for namespace constants?

* support for "import ns::*"?

* performance problems

- calls to internal functions in namespaces are slower, because PHP first
lools for such function in current namespace

looks for such function in current namespace
- calls to static methods are slower, because PHP first tries to look
for corresponding function in namespace

* Extend the Reflection API

* Add ReflectionPackage class

* Extend the Reflection API?
* Add ReflectionNamespace class
+ getName()
+ getClasses()
+ getFunctions()
+ getFiles()

* Add getNamespace() methods to ReflectionClass and ReflectionFunction

* Rename namespaces to packages?

0 comments on commit d92a0aa

Please sign in to comment.