rfc:php_namespace_policy

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Last revision Both sides next revision
rfc:php_namespace_policy [2020/06/23 21:02]
crell Overhaul wording to make it more prescriptive and precise
rfc:php_namespace_policy [2020/08/10 13:38]
crell
Line 3: Line 3:
   * Date: 2020-04-15 ​   * Date: 2020-04-15 ​
   * Author: Mark Randall (marandall@php.net),​ Larry Garfield (larry@garfieldtech.com)   * Author: Mark Randall (marandall@php.net),​ Larry Garfield (larry@garfieldtech.com)
-  * Status: ​Under Discussion+  * Status: ​In Voting
   * First Published at: https://​wiki.php.net/​rfc/​php_namespace_policy   * First Published at: https://​wiki.php.net/​rfc/​php_namespace_policy
  
Line 12: Line 12:
 This RFC proposes to: This RFC proposes to:
  
-* Formally reserve the \PHP namespace for use by PHP-SRC. ​ It has been quasi-soft-reserved since namespaces were added, but never used because we couldn'​t agree on when or how to do so.  So, let's do so.+  ​* Formally reserve the \PHP namespace for use by PHP-SRC. ​ It has been quasi-soft-reserved since namespaces were added, but never used because we couldn'​t agree on when or how to do so.  So, let's do so.
  
-Establish heuristics regarding when and how namespaced classes should be used.  In practice there cannot be precise rules in advance ​for every casebut by offering a common heuristic we hope to eliminate //most// purely subjective debate ​in the future.+  ​Formally reserve the \Ext namespace ​for use by extensionseither PECL or in core.
  
-* Propose guidelines for how existing PHP-SRC-provied ​classes MAY be migrated to namespaces in the future by other RFCs.+  * Establish heuristics regarding when and how namespaced classes should be used.  In practice there cannot be precise rules in advance for every case, but by offering a common heuristic we hope to eliminate //most// purely subjective debate in the future. 
 + 
 +  ​* Propose guidelines for how existing PHP-SRC-provided ​classes MAY be migrated to namespaces in the future by other RFCs.
  
  
 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. 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 =====
 +
 +  * **Vendor namespace**:​ The top-level namespace in a symbol name.  For example, in \Foo\Bar\Baz,​ "​Foo"​ is the vendor.
 +
 +  * **Component namespace**:​ The second level namespace in a symbol name.  For example, in \Foo\Bar\Baz,​ "​Bar"​ is the component namespace.
 +
 +  * **Class**: For the purpose of this document, "​class"​ means any autoloadable class-like symbol definition. ​ At this time that means classes, interfaces, and traits. ​ Future data structure definitions (Enums, typedefs, etc.) MAY be namespaced if they are made autoloadable. ​ This is to make it easier to write polyfills for new functionality in the future.
  
 ===== Namespace Policy ===== ===== Namespace Policy =====
  
-1. The \PHP 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.+  - 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
 +  - 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. 
 +  - 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. 
 +  - Component or sub-component namespaces MUST use CamelCase naming conventions. 
 +  - Only classes and other autoloadable symbols (as specified above) may be namespaced. ​ Constants should be associated to a class within the namespace as appropriate. 
 +  - 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. 
 +  - 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.
  
-2. Any namespaced code provided by PHP-SRC will use a sub-namespace of \PHP.  That is, no PHP\Foo class may be defined, but a PHP\Foo\Bar class may be.  The sub-namespace is referred to as a "​component namespace"​ for the rest of this document.  ​+===== Namespace index =====
  
-3. 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\Attributes is the namespace for attributes, additional attribute classes may be defined there but no non-attribute-related classes may be.+page will be established on the PHP Wiki (this site) to index all explicitly specified ​component ​namespaces for both \PHP and \Ext.
  
-4. 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.+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.
  
-5. If a feature is removed from PHP-SRC (either to PECL or dropped entirely) its previously claimed ​component namespace ​remains reserved It MAY be released ​by a subsequent ​RFC, following ​the standard RFC procedureat least one minor version after the feature is removed.  ​That delay is to minimize backward compatibility impact and allow userland code to migrate if appropriate.+A \PHP component namespace ​is "​claimed"​ by the RFC that first uses itOnce claimed, that component namespace may only be used only by that RFC or future RFCs that extend that one in a natural way.  For exampleonce it's established that \PHP\Attribute\ is the namespace for attributesadditional 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.
  
-6. Only autoloadable symbols may be namespaced. ​ At this time that means classes, interfaces, and traits. ​ Constants should be associated to a class within the namespace ​as appropriate. ​ Future data structure definitions (Enums, typedefs, etc.) MAY be namespaced if they are made autoloadable. ​ This is to make it easier to write polyfills for new functionality ​in the future.+An \Ext component ​namespace is "​claimed" ​in one of three ways:
  
-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.+  - An RFC that creates ​or migrates ​bundled extension. 
 +  - 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
 +  - PECL module may automatically claim a namespace ​based on its package name.  For examplea "​yoursql"​ database driver in PECL would implicitly claim the \Ext\YourSQL namespace. 
 +  - 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 ===== ===== Guidelines for namespace usage =====
Line 43: Line 62:
 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. 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 that provide engine-level behaviour, such as base types: ​**GLOBAL** +Classes ​or interfaces ​that impact the type system in ways that affect runtime behavior 
-  ​* Traversable ​Iterable +  ​* **Examples**TraversableIterableCountableArrayAccess, WeakReference, ​Attribute 
-  * Countable +  * **Guideline**:​ These interfaces should remain global
-  * ArrayAccess +
-  * Attribute +
-  * WeakReference+
  
-Classes that extend ​engine-level behaviours or provide special-casing: ​**NAMESPACE** +Classes ​or interfaces ​that extend ​a type the previous group 
-  ​Specialised Iterators +  ​* **Examples**: The various ​*Iterator classes, specific Attribute instances 
-  ​Attributes Depreciated / JIT etc +  * **Guideline**:​ A namespace specific to the parent. ​ Eg, \PHP\Iterator\,​ \PHP\Attribute\. ​ As new instances are added they would be added to that component namespace.
-  * High-Performance Tokenization+
  
-Classes ​providing core functionality ​that would not be considered specific to an extension: **NAMESPACE** +Classes that are part of an extension, disableable or not. 
-  * SPL (and its successor) ​ +  * **Examples**SimpleXMLElement,​ SoapClient, Sodium, DOM, SPL 
-  * Collections / Future Generics Implementations of Collections+  ​* **Guideline**: An extension namespace specific to the use case.  Eg, \Ext\Sodium. 
 + 
 +Cases where it is expected that the number of closely related classes is or is likely to become large 
 +  * **Examples**:​ DOM, Attributes, core Exceptions 
 +  * **Guideline**:​ A namespace specific to the use case. 
 + 
 +Cases not covered above: 
 +  * Global, unless a good argument is made to the contrary.
  
 ===== Upgrade path for existing classes ===== ===== 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, and put to a vote on their own merits. ​ Such RFCs must:+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. 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.
Line 69: Line 91:
  
 ===== Proposed PHP Version(s) ===== ===== Proposed PHP Version(s) =====
-PHP 8.0+PHP 8.0 (doesn'​t really have an impact until 8.1, however)
  
 ===== Vote ===== ===== Vote =====
-Yes / No  
  
-"​Should RFC authors ​be permitted ​to utilise a sub-namespace of \PHP to contain their features' ​userland accessible classes, traits, interfaces etc" ​+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?"​ 
 + 
 + 
 +<doodle title="​Adopt this policy for future symbols defined by php-src and extension code?" auth="​crell"​ voteType="​single"​ closed="​true"
 +   * Yes 
 +   * No 
 +</​doodle>​
  
 ===== Prior Art ==== ===== Prior Art ====
 https://​wiki.php.net/​rfc/​namespaces-in-core (withdrawn) https://​wiki.php.net/​rfc/​namespaces-in-core (withdrawn)
  
rfc/php_namespace_policy.txt · Last modified: 2020/08/10 13:41 by crell