rfc:namespaceref

Namespace refinement proposals

The purpose of the RFC is to summaries current proposals regarding namespaces.

Introduction

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:

  1. 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();
  1. Full name not mentioned in import - Foo::Bar::Baz - take at the face value. Special cases of this are:
    1. ::Foo - global name, i.e. explicit reference to non-namespaced class inside namespace
    2. namespace::Foo - name explicitly referring to current namespace
  2. Unqualified name “Foo” not mentioned in import - resolved as namespace::Foo.

Pro

  • 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.

Contra

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:

  1. All possible “use” imports are resolved against A (first component).
  2. If function by name “A::B::C” exists, take A::B::C as the function name and do the regular function call.
  3. Otherwise, treat the call as call to method “C” of the class “A::B”.

Unqualified function call foo() inside namespace resolved as follows:

  1. If internal function “foo” exists - call the function
  2. Otherwise, call the function namespace::foo

Constants resolved in a way identical to functions.

New syntax for static access is introduced: using Name->Member is the same as Name::Member, e.g.:

ClassName->Foo() - static method call
ClassName->$Foo - static property access
ClassName->Foo - class constant access

Pro

  • The last syntax allows to call static methods unambiguously
  • Functions and constants supported

Contra

  • 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

See also

Please see also Namespace RFC for more details

rfc/namespaceref.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1