Namespace refinement proposals
- Version: 1.0
- Date: 2008-03-06
- Author: Stas Malyshev firstname.lastname@example.org
- Status: Under Discussion
- First Published at: http://wiki.php.net/rfc/namespaceref
The purpose of the RFC is to summaries current proposals regarding namespaces.
So far I have two proposals for resolving current namespace debates - one that includes namespaced function and one that does not. This RFC assumes you are familiar with the topic of namespaces in general.
Namespaces without functions/constants
All new functionality relates only to classes. Any code is allowed inside namespace, but only class definitions and references to classes is influenced by it. Class defined in the namespace is prefixed by it's name.
Inside namespace, class definition can be used as:
- Imported name - name mentioned in “use” statement as shortcut for the full name. Imported name can be also a prefix to further qualified name.
use Foo::Bar as Fubar; $a = new Fubar::Baz();
- Full name not mentioned in import -
Foo::Bar::Baz- take at the face value. Special cases of this are:
::Foo- global name, i.e. explicit reference to non-namespaced class inside namespace
namespace::Foo- name explicitly referring to current namespace
- Unqualified name “Foo” not mentioned in import - resolved as
- Most (not all, I know, but most) of the use cases for namespaces are in the OO realm, and most of the problems they are to serve come from that realm too. So at least initially most of the active users, which wait for it impatiently, are OO users, and classes are the thing the care the most about.
- Everything becomes so much simpler with only classes. Classes and functions have very different usage patterns in PHP, so if we try to serve them both we inevitably encounter some “inconsistencies” in how they are served, because of the different usage patterns.
Well, duh, no functions :)
Namespaces with functions/constants
As the above, plus:
Functions and constants defined in the namespace are prefixed with the namespace name.
Quailified function call
A::B::C() is resolved as follows:
- All possible “use” imports are resolved against A (first component).
- If function by name “A::B::C” exists, take
A::B::Cas the function name and do the regular function call.
- Otherwise, treat the call as call to method “C” of the class “A::B”.
Unqualified function call
foo() inside namespace resolved as follows:
- If internal function “foo” exists - call the function
- Otherwise, call the function
Constants resolved in a way identical to functions.
New syntax for static access is introduced: using
Name->Member is the same as
ClassName->Foo() - static method call ClassName->$Foo - static property access ClassName->Foo - class constant access
- The last syntax allows to call static methods unambiguously
- Functions and constants supported
- The model is more complex and may be confusing for the new users.
- Unqualified name resolutions for classes and functions/constants are slightly different due to the different usage patterns.
- New syntax for static access
Please see also Namespace RFC for more details