Table of Contents

PHP RFC: PHP Namespace Policy

Proposal

PHP-SRC provides an ever-increasing number of userland-accessible classes. The recent addition of Attributes is very likely to result in even more in the future. Every new global class, however, creates a potential namespace collision with existing user-space code and thus a potential for backward compatibility breaks.

This RFC proposes to:

This RFC does NOT propose moving any existing code into the \PHP namespace at this time. That may be done by future RFCs if desired, under their own votes. This is a “policy document” only.

Definitions

Namespace Policy

  1. The \PHP vendor namespace is reserved for use by classes provided by PHP-SRC itself. While userland code that makes use of that namespace will technically run, any impact on such code by future RFCs will not be considered a backward compatibility break.
  2. The \Ext vendor namespace is reserved for use by classes provided by a PHP extension, either bundled with PHP or hosted with PECL. While userland code that makes use of that namespace will technically run, any impact on such code by future RFCs will not be considered a backward compatibility break.
  3. Any namespaced code provided by PHP-SRC will use a distinct component namespace. That is, no PHP\Foo class may be defined, but a PHP\Foo\Bar class may be.
  4. Component or sub-component namespaces MUST use CamelCase naming conventions.
  5. Only classes and other autoloadable symbols (as specified above) may be namespaced. Constants should be associated to a class within the namespace as appropriate.
  6. Component namespaces MAY use sub-components (eg, \PHP\Foo\Bar\), but that SHOULD be discouraged unless there is clear and convincing evidence that it would aid in readability or code organization.
  7. Classes or other symbols in a component namespace SHOULD NOT repeat the component namespace, unless the class name is extremely generic and easily misunderstood without context. This requires case-by-case evaluation but the default is to not repeat the namespace without strong justification.

Namespace index

A page will be established on the PHP Wiki (this site) to index all explicitly specified component namespaces for both \PHP and \Ext.

For \PHP, classes may be added or removed only via RFC. That may be an RFC to add a feature that uses the namespace or it may be an RFC to rename an existing class to conform to this RFC.

A \PHP component namespace is “claimed” by the RFC that first uses it. Once claimed, that component namespace may only be used only by that RFC or future RFCs that extend that one in a natural way. For example, once it's established that \PHP\Attribute\ is the namespace for attributes, additional attribute classes may be defined there but no non-attribute-related classes may be. Once the RFC is approved, the namespace should be added to the index.

An \Ext component namespace is “claimed” in one of three ways:

  1. An RFC that creates or migrates a bundled extension.
  2. A PECL module author may post to the Internals maliing list stating an intent to “claim” an \Ext component namespace for a package. If there is no negative response within a week, the author may update the index accordingly. If there is, the author may post an RFC with vote to claim the component.
  3. PECL module may automatically claim a namespace based on its package name. For example, a “yoursql” database driver in PECL would implicitly claim the \Ext\YourSQL namespace.
  4. In case of conflict, an RFC-claimed component name has priority over an explicitly-claimed component name over an implicitly-claimed component name.

Guidelines for namespace usage

The following heuristics are intended to guide the review of future RFCs by providing a consistent pattern for naming. They are by nature not hard and precise rules, but future RFCs SHOULD follow them as closely as possible.

Examples of existing classes or components listed below are for explanatory and demonstration purposes only. This RFC does not propose to rename them here. Future RFCs may do so if desired, following the process outlined below.

Classes or interfaces that impact the type system in ways that affect runtime behavior

Classes or interfaces that extend a type the previous group

Classes that are part of an extension, disableable or not.

Cases where it is expected that the number of closely related classes is or is likely to become large

Cases not covered above:

Upgrade path for existing classes

This RFC makes no change to existing classes, regardless of where they are defined. Future RFCs may be proposed to migrate existing classes to a component namespace within \PHP or \Ext as appropriate, and put to a vote on their own merits. Such RFCs must:

1. Clearly specify what classes or interfaces will migrate to a component namespace, including whether or not their name will be changing in the process. 2. Include an alias, subclass, or other backward compatibility shim to ensure existing userland code remains working without change.

Removal of the older, un-namespaced symbols must happen in a separate RFC, and MUST happen no earlier than one full release cycle after that introduction of the namespaced symbol. That is, a rename introduced in PHP 8.2 is not eligible to have its old name removed until at least 10.0.

Proposed PHP Version(s)

PHP 8.0 (doesn't really have an impact until 8.1, however)

Vote

Voting opened 2020-07-26 and closes 2020-08-09.

Yes / No vote, requiring 2/3 to pass.

“Should PHP adopt these guidelines for future RFC authors to guide when and how to use the \PHP and \Ext namespaces for userland-accessible classes and similar?”

Adopt this policy for future symbols defined by php-src and extension code?
Real name Yes No
alcaeus (alcaeus)  
alec (alec)  
ashnazg (ashnazg)  
beberlei (beberlei)  
carusogabriel (carusogabriel)  
colinodell (colinodell)  
crell (crell)  
danack (danack)  
derick (derick)  
dragoonis (dragoonis)  
galvao (galvao)  
jasny (jasny)  
kalle (kalle)  
kelunik (kelunik)  
kocsismate (kocsismate)  
lcobucci (lcobucci)  
malukenho (malukenho)  
marandall (marandall)  
mauricio (mauricio)  
mbeccati (mbeccati)  
mcmic (mcmic)  
nicolasgrekas (nicolasgrekas)  
ocramius (ocramius)  
ramsey (ramsey)  
reywob (reywob)  
salathe (salathe)  
sergey (sergey)  
stas (stas)  
twosee (twosee)  
yunosh (yunosh)  
Final result: 13 17
This poll has been closed.

Prior Art

https://wiki.php.net/rfc/namespaces-in-core (withdrawn)