PHP Coding Standards (PSR)

Lecture



PSR-0: Class Name Requirements

and neymspeysov for universal autoloader.
The full class name should be of the form `\ <Vendor Name> \ (<Namespace> \) * <Class Name>`


`\Symfony\Core\Request` => `/path/to/project/lib/vendor/Symfony/Core/Request.php`
`\Zend\Acl` => `/path/to/project/lib/vendor/Zend/Acl.php`
`\namespace\package_name\Class_Name` => `/path/to/project/lib/vendor/namespace/package_name/Class/Name.php`



PSR-1: Basic Coding Standards

The section describes the general code design rules:

- Use only tags <? Php and <? =
- Only UTF-8 without BOM for pkhp code
- Do not interfere with different functionality in one file
- NS and classes must follow psr-0
- classes are announced in `StudlyCase`
- methods are declared in lower `camelCase`
- constants are declared in TAKOM_VIDE

There are no requirements for class properties; they can be declared in any form,
it is important that the style is the same for the whole project.

PSR-2: Code Execution Standards

Actually represents the requirements for the design of the code:

- the code should use 4 spaces instead of TAB to separate the code

- the length of the string is no more than 80 characters, well 120, at most

- empty line after declaration of NS and USE block

- the opening bracket `{` class and method declarations should be on the next line, and the closing `}` on the next line after the code

- for all properties and methods, it is necessary to specify the visibility;
the keywords `abstract` and` final` are specified before visibility is declared, and `static` after

- After control keywords (commands of a language) a space should be put, not put after calling methods and functions

- The opening brackets `{` for commands are put on the same line with the keyword, and the closing `}` on the next line after the code

- Command brackets `()` should not begin and end with spaces


Design Example:



<?php
namespace Vendor\Package;

use FooInterface;
use BarClass as Bar;
use OtherVendor\OtherPackage\BazClass;

class Foo extends Bar implements FooInterface
{
public function sampleFunction($a, $b = null)
{
if ($a === $b) {
bar();
} elseif ($a > $b) {
$foo->bar($arg1);
} else {
BazClass::bar($arg2, $arg3);
}
}

final public static function bar()
{
// method body
}
}

PSR-3-logger-interface.md

Logger interface

This document describes a common interface for logging libraries.

It is a simple and universal way. SHOULD remain compatible with this document. This will ensure that you can use the centralized application logs.

The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119.

It is interpreted to be interpreted in a document-related library or framework. Users of loggers are referred to as user .

1. Specification

1.1 Basics

  • RFC 5424 levels (debug, info, notice, warning, error, critical, alert, emergency).

  • A ninth method, log , accepts a log level as first argument. Constants MUST have the same result. MUST throw a Psr\Log\InvalidArgumentException . SHOULD you use it.

1.2 Message

  • Every method accepts a string with a __toString() method. Implementors MAY have special handling for the passed objects. If it is not the case, implementors MUST cast it to a string.

  • The message MAY contains placeholders which replace the values ​​from the context array.

    Placeholder names MUST correspond to keys in the context array.

    Placeholder names MUST be delimited with a single opening brace. There must not be any whitespace between the delimiters and the placeholder name.

    Placeholder names SHOULD be composed only of the characters AZ , az , 0-9 , underscore _ , and period . . It is reserved.

    Implementors MAY use placeholders to implement various escaping strategies and translate logs for display. SHOULD NOT pre-escape placeholder values ​​since the data will be displayed.

    The following is an example of implementation of placeholder interpolation provided for reference purposes:

      / **
     * Interpolates context values ​​into the message placeholders.
     * /
     function interpolate ($ message, array $ context = array ())
     {
         // build a replacement array with braces around the context keys
         $ replace = array ();
         foreach ($ context as $ key => $ val) {
             $ replace ['{'.  $ key.  '}'] = $ val;
         }
    
         // interpolate replacement values ​​into the message and return
         return strtr ($ message, $ replace);
     }
    
     // a message with brace-delimited placeholder names
     $ message = "User {username} created";
    
     // a context array of placeholder names => replacement values
     $ context = array ('username' => 'bolivar');
    
     // echoes "User bolivar created"
     echo interpolate ($ message, $ context); 

1.3 Context

  • Every method accepts an array as context data. This is not a very well-defined information. The array can contain anything. Implementors MUST ensure that it is possible. Must have a note of interest, a warning or warning.

  • MUST be in the 'exception' key. For example, it supports the logging exceptions. Implementors MUST still verify that the MAY contain anything.

1.4 Helper classes and interfaces

  • The Psr\Log\AbstractLogger class allows you to easily implement the LoggerInterface . Context of it.

  • Similarly, using the Psr\Log\LoggerTrait the generic log method. Note that since you can not implement interfaces, LoggerInterface .

  • The Psr\Log\NullLogger is provided together with the interface. It’s possible to use it. However, the context data creation is expensive.

  • The Psr\Log\LoggerAwareInterface only contains a setLogger(LoggerInterface $logger) .

  • The Psr\Log\LoggerAwareTrait trait can be used to implement it. It gives you access to $this->logger .

  • The Psr\Log\LogLevel class holds the eight log levels.

2. Package

This is the case in the psr / log package.

3. Psr\Log\LoggerInterface

  <? php

 namespace Psr \ Log;

 / **
  * Describes a logger instance
  *
  * The message MUST be a string or object implementing __toString ().
  *
  * The message MAY contain placeholders in the form: {foo} where foo
  * will be replaced by the context data in key "foo".
  *
  * The context array can be arbitrary data assumption that
  * Except instance is given.
  * to produce a stack trace, it must be in a key named "exception".
  *
  * See https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md
  * for the full interface specification.
  * /
 interface LoggerInterface
 {
     / **
      * System is unusable.
      *
      * @param string $ message
      * @param array $ context
      * @return null
      * /
     public function emergency ($ message, array $ context = array ());

     / **
      * Action must be taken immediately.
      *
      * Example: Entire website down, database unavailable, etc.  This should
      * trigger SMS alerts and wake you up.
      *
      * @param string $ message
      * @param array $ context
      * @return null
      * /
     public function alert ($ message, array $ context = array ());

     / **
      * Critical conditions.
      *
      * Example: Application component unavailable, unexpected exception.
      *
      * @param string $ message
      * @param array $ context
      * @return null
      * /
     public function critical ($ message, array $ context = array ());

     / **
      * Runtime errors typically
      * be logged and monitored.
      *
      * @param string $ message
      * @param array $ context
      * @return null
      * /
     public function error ($ message, array $ context = array ());

     / **
      * Exceptional occurrences that are not errors.
      *
      * Example: use of deprecated APIs, poor use of an API, undesirable things
      * that are not necessarily wrong.
      *
      * @param string $ message
      * @param array $ context
      * @return null
      * /
     public function warning ($ message, array $ context = array ());

     / **
      * Normal but significant events.
      *
      * @param string $ message
      * @param array $ context
      * @return null
      * /
     public function notice ($ message, array $ context = array ());

     / **
      * Interesting events.
      *
      * Example: User logs in, SQL logs.
      *
      * @param string $ message
      * @param array $ context
      * @return null
      * /
     public function info ($ message, array $ context = array ());

     / **
      * Detailed debug information.
      *
      * @param string $ message
      * @param array $ context
      * @return null
      * /
     public function debug ($ message, array $ context = array ());

     / **
      * Logs with an arbitrary level.
      *
      * @param mixed $ level
      * @param string $ message
      * @param array $ context
      * @return null
      * /
     public function log ($ level, $ message, array $ context = array ());
 } 

4. Psr\Log\LoggerAwareInterface

  <? php

 namespace Psr \ Log;

 / **
  * Describes a logger-aware instance
  * /
 interface LoggerAwareInterface
 {
     / **
      * Sets a logger instance on the object
      *
      * @param LoggerInterface $ logger
      * @return null
      * /
     public function setLogger (LoggerInterface $ logger);
 } 

5. Psr\Log\LogLevel

  <? php

 namespace Psr \ Log;

 / **
  * Describes log levels
  * /
 class LogLevel
 {
     const EMERGENCY = 'emergency';
     const ALERT = 'alert';
     const CRITICAL = 'critical';
     const ERROR = 'error';
     const WARNING = 'warning';
     const NOTICE = 'notice';
     const INFO = 'info';
     const DEBUG = 'debug';
 } 


Example Implementations of PSR-4

The following are examples illustrate PSR-4 compliant code:

Closure Example

  <? php
 / **
  * An example of a project-specific implementation.
  * 
  * After registering this autoload function with SPL, the following line
  * Foo \ Bar \ Baz \ Qux class
  * from /path/to/project/src/Baz/Qux.php:
  * 
  * new \ Foo \ Bar \ Baz \ Qux;
  *      
  * @param string $ class The fully-qualified class name.
  * @return void
  * /
 spl_autoload_register (function ($ class) {

     // project-specific namespace prefix
     $ prefix = 'Foo \\ Bar \\';

     // base directory for the namespace prefix
     $ base_dir = __DIR__.  '/ src /';

     // does the class use the namespace prefix?
     $ len = strlen ($ prefix);
     if (strncmp ($ prefix, $ class, $ len)! == 0) {
         // no, move to the next registered autoloader
         return;
     }

     // get the relative class name
     $ relative_class = substr ($ class, $ len);

     // replace the namespace prefix with the base directory, replace namespace
     // separators with directory, directory, append
     // with .php
     $ file = $ base_dir.  str_replace ('\\', '/', $ relative_class).  '.php';

     // if the file exists, require it
     if (file_exists ($ file)) {
         require $ file;
     }
 }); 

Class Example

The following is an example of multiple class namespaces:

  <? php
 namespace Example;

 / **
  * An example of a general-purpose implementation that includes the optional
  * functionality of the multiple namespace
  * prefix.
  * 
  * Given the foo-bar package at the following
  * paths ...
  * 
  * / path / to / packages / foo-bar /
  * src /
  * Baz.php # Foo \ Bar \ Baz
  * Qux /
  * Quux.php # Foo \ Bar \ Qux \ Quux
  * tests /
  * BazTest.php # Foo \ Bar \ BazTest
  * Qux /
  * QuuxTest.php # Foo \ Bar \ Qux \ QuuxTest
  * 
  * ... add the prefix files to the \ Foo \ Bar \ namespace
  * as follows:
  * 
  * <? php
  * // instantiate the loader
  * $ loader = new \ Example \ Psr4AutoloaderClass;
  *      
  * // register the autoloader
  * $ loader-> register ();
  *      
  * // register the base directories for the namespace prefix
  * $ loader-> addNamespace ('Foo \ Bar', '/ path / to / packages / foo-bar / src');
  * $ loader-> addNamespace ('Foo \ Bar', '/ path / to / packages / foo-bar / tests');
  * 
  The following line would have been
  * \ Foo \ Bar \ Qux \ Quux class from /path/to/packages/foo-bar/src/Qux/Quux.php:
  * 
  * <? php
  * new \ Foo \ Bar \ Qux \ Quux;
  * 
  The following line would have been 
  * \ Foo \ Bar \ Qux \ QuuxTest class from /path/to/packages/foo-bar/tests/Qux/QuuxTest.php:
  * 
  * <? php
  * new \ Foo \ Bar \ Qux \ QuuxTest;
  * /
 class Psr4AutoloaderClass
 {
     / **
      * An associative array where the key is a namespace prefix and the value
      * is an array of base directories for classes in that namespace.
      *
      * @var array
      * /
     protected $ prefixes = array ();

     / **
      * Register loader with SPL autoloader stack.
      * 
      * @return void
      * /
     public function register ()
     {
         spl_autoload_register (array ($ this, 'loadClass'));
     }

     / **
      * Adds a base directory for a namespace prefix.
      *
      * @param string $ prefix The namespace prefix.
      * @param string $ base_dir A base directory for class files in
      * namespace.
      * @param bool prepend If true, prepend the base directory to the stack
      * instead of appending it;  first cause
      * than last.
      * @return void
      * /
     public function addNamespace ($ prefix, $ base_dir, $ prepend = false)
     {
         // normalize namespace prefix
         $ prefix = trim ($ prefix, '\\').  '\\';

         // normalize the base directory with a trailing separator
         $ base_dir = rtrim ($ base_dir, DIRECTORY_SEPARATOR).  '/';

         // initialize the namespace prefix array
         if (isset ($ this-> prefixes [$ prefix]) === false) {
             $ this-> prefixes [$ prefix] = array ();
         }

         // retain the base directory for the namespace prefix
         if ($ prepend) {
             array_unshift ($ this-> prefixes [$ prefix], $ base_dir);
         } else {
             array_push ($ this-> prefixes [$ prefix], $ base_dir);
         }
     }

     / **
      * Loads the class file for a given class name.
      *
      * @param string $ class The fully-qualified class name.
      * @return mixed
      * failure.
      * /
     public function loadClass ($ class)
     {
         // the current namespace prefix
         $ prefix = $ class;

         // work backwards through the namesspace of the fully-qualified
         // class name to find a mapped file name
         while (false! == $ pos = strrpos ($ prefix, '\\')) {

             // retain the trailing namespace separator in the prefix
             $ prefix = substr ($ class, 0, $ pos + 1);

             // the rest is the relative class name
             $ relative_class = substr ($ class, $ pos + 1);

             // try to load a mapped file for the prefix and relative class
             $ mapped_file = $ this-> loadMappedFile ($ prefix, $ relative_class);
             if ($ mapped_file) {
                 return $ mapped_file;
             }

             // remove the trailing namespace separator for the next iteration
             // of strrpos ()
             $ prefix = rtrim ($ prefix, '\\');   
         }

         // never found a mapped file
         return false;
     }

     / **
      * Load the mapped file for a namespace prefix and relative class.
      * 
      * @param string $ prefix The namespace prefix.
      * @param string $ relative_class The relative class name.
      * @return mixed Boolean if no mapped file can be loaded, or the
      * name of the mapped file that was loaded.
      * /
     protected function loadMappedFile ($ prefix, $ relative_class)
     {
         // are there any base directories for this namespace prefix?
         if (isset ($ this-> prefixes [$ prefix]) === false) {
             return false;
         }

         // look through base directories for this namespace prefix
         foreach ($ this-> prefixes [$ prefix] as $ base_dir) {

             // replace the namespace prefix with the base directory,
             // replace namespace separators with directory separators
             // in the relative class name, append with .php
             $ file = $ base_dir
                   .  str_replace ('\\', '/', $ relative_class)
                   .  '.php';

             // if the mapped file exists, require it
             if ($ this-> requireFile ($ file)) {
                 // yes, we're done
                 return $ file;
             }
         }

         // never found it
         return false;
     }

     / **
      * If a file exists, require it from the file system.
      * 
      * @param string $ file The file to require.
      * @return bool True if the file exists, false if not.
      * /
     protected function requireFile ($ file)
     {
         if (file_exists ($ file)) {
             require $ file;
             return true;
         }
         return false;
     }
 } 

Unit tests

The following example is one way of loader:

  <? php
 namespace Example \ Tests;

 class MockPsr4AutoloaderClass extends Psr4AutoloaderClass
 {
     protected $ files = array ();

     public function setFiles (array $ files)
     {
         $ this-> files = $ files;
     }

     protected function requireFile ($ file)
     {
         return in_array ($ file, $ this-> files);
     }
 }

 class Psr4AutoloaderClassTest extends \ PHPUnit_Framework_TestCase
 {
     protected $ loader;

     protected function setUp ()
     {
         $ this-> loader = new MockPsr4AutoloaderClass;

         $ this-> loader-> setFiles (array (
             '/vendor/foo.bar/src/ClassName.php',
             '/vendor/foo.bar/src/DoomClassName.php',
             '/vendor/foo.bar/tests/ClassNameTest.php',
             '/vendor/foo.bardoom/src/ClassName.php',
             '/vendor/foo.bar.baz.dib/src/ClassName.php',
             '/vendor/foo.bar.baz.dib.zim.gir/src/ClassName.php',
         ))

         $ this-> loader-> addNamespace (
             'Foo \ bar',
             '/vendor/foo.bar/src'
         );

         $ this-> loader-> addNamespace (
             'Foo \ bar',
             '/vendor/foo.bar/tests'
         );

         $ this-> loader-> addNamespace (
             'Foo \ BarDoom',
             '/vendor/foo.bardoom/src'
         );

         $ this-> loader-> addNamespace (
             'Foo \ Bar \ Baz \ Dib',
             '/vendor/foo.bar.baz.dib/src'
         );

         $ this-> loader-> addNamespace (
             'Foo \ Bar \ Baz \ Dib \ Zim \ Gir',
             '/vendor/foo.bar.baz.dib.zim.gir/src'
         );
     }

     public function testExistingFile ()
     {
         $ actual = $ this-> loader-> loadClass ('Foo \ Bar \ ClassName');
         $ expect = '/vendor/foo.bar/src/ClassName.php';
         $ this-> assertSame ($ expect, $ actual);

         $ actual = $ this-> loader-> loadClass ('Foo \ Bar \ ClassNameTest');
         $ expect = '/vendor/foo.bar/tests/ClassNameTest.php';
         $ this-> assertSame ($ expect, $ actual);
     }

     public function testMissingFile ()
     {
         $ actual = $ this-> loader-> loadClass ('No_Vendor \ No_Package \ NoClass');
         $ this-> assertFalse ($ actual);
     }

     public function testDeepFile ()
     {
         $ actual = $ this-> loader-> loadClass ('Foo \ Bar \ Baz \ Dib \ Zim \ Gir \ ClassName');
         $ expect = '/vendor/foo.bar.baz.dib.zim.gir/src/ClassName.php';
         $ this-> assertSame ($ expect, $ actual);
     }

     public function testConfusion ()
     {
         $ actual = $ this-> loader-> loadClass ('Foo \ Bar \ DoomClassName');
         $ expect = '/vendor/foo.bar/src/DoomClassName.php';
         $ this-> assertSame ($ expect, $ actual);

         $ actual = $ this-> loader-> loadClass ('Foo \ BarDoom \ ClassName');
         $ expect = '/vendor/foo.bardoom/src/ClassName.php';
         $ this-> assertSame ($ expect, $ actual);
     }
 } 

PSR-4 Meta Document

1. Summary

Autoloader php registered autoloader. This would be an addition to, not a replacement for, PSR-0.

2. Why Bother?

History of PSR-0

The PSR-0 convention is under the limitation of php 5.2 and previous. If you want to go to your pseudo-namespaces, like this:

 /path/to/src/ VendorFoo/ Bar/ Baz.php # VendorFoo_Bar_Baz VendorDib/ Zim/ Gir.php # Vendor_Dib_Zim_Gir 

It was a rule that the phrases were released under the name of a new namespace notation. Underscores were encouraged to accept wider adoption.

 /path/to/src/ VendorFoo/ Bar/ Baz.php # VendorFoo_Bar_Baz VendorDib/ Zim/ Gir.php # VendorDib_Zim_Gir Irk_Operation/ Impending_Doom/ V1.php V2.php # Irk_Operation\Impending_Doom\V2 

If you are a installer, you must be able to get the installer back into the single central directory.

Along comes composer

With Composer, package sources are no longer copied to a single global location. They are not used around. Comparser for PHP sources as with PEAR. Instead, there are multiple directories; each package is a separate directory for each project.

To meet the requirements of the PSR-0, this leads to Composer packages looking like this:

 vendor/ vendor_name/ package_name/ src/ Vendor_Name/ Package_Name/ ClassName.php # Vendor_Name\Package_Name\ClassName tests/ Vendor_Name/ Package_Name/ ClassNameTest.php # Vendor_Name\Package_Name\ClassNameTest 

The "src" and "tests" directories have the directory names and package directory names. This is an artifact of PSR-0 compliance.

Deeper and more-repetitive than necessary. This proposal suggests that it would be useful to

 vendor/ vendor_name/ package_name/ src/ ClassName.php # Vendor_Name\Package_Name\ClassName tests/ ClassNameTest.php # Vendor_Name\Package_Name\ClassNameTest 

"Package-oriented autoloading" (as vs the traditional "direct class-to-file autoloading").

Package-Oriented Autoloading

It is difficult to implement an agreement or amendment to the PSR-0, because the PSR-0 doesn’t need it. This would be more complicated than the PSR-0. However, it would allow for cleaner packages.

Initially, the following rules were suggested:

  1. Implementors MUST use at least two namespace levels: (This is a combination of the two-name combination of the name and the package namespace.)

  2. Implementors MUST BE FUNDS:

  3. MAY map to any directory. The remaining portion of the number is the identity portion of the number of identically-defined names.

Note that this is the end of the underscore-as-directory-separator in the class name. It would be possible to note that it would be a rule.

3. Scope

3.1 Goals

  • Retain the PSR-0 rule that you need to use at least two namespace levels:

  • Allow a path infix between the fully qualified class name.

  • Allow the package directory namespace.

  • End directory

3.2 Non-Goals

  • Non-class resources

4. Approaches

4.1 Chosen Approach

This eliminates the deeper directory structure. In addition, it specifies more specific rules that make implementations explicitly more interoperable.

Although autoloaders should handle errors. Specifically, it forbids throwing exceptions or raising errors. The reason is two-fold.

  1. Autoloaders in PHP are explicitly designed to be stackable so that if you can’t get it, it’s not possible. Having an autoloader condition violates that compatibility.

  2. class_exists() and interface_exists() allow "not found, even after trying to autoload" as a legitimate, normal use case. An autoloader that throws exceptions renders class_exists() unusable, which is completely unacceptable from an interoperability standpoint. It is not necessary to ensure that there is a need for a log of information.

Pros:

  • Shallower directory structures

  • More flexible file locations

  • Stops underscore in class name from being honored as directory separator

  • Makes implementations more explicitly interoperable

Cons:

  • It is not necessary to conventionally inherit from Horde / PEAR.

4.2 Alternative: Stay With PSR-0 Only

Staying with PSR-0 for relatively deeper directory structures.

Pros:

  • Habits or implementations

Cons:

  • Leaves us with deeper directory structures

  • Leaves us with underscores in the class name being honored as directory separators

4.3 Alternative: Split Up Autoloading And Transformation

It couldn’t be referenced by other proposals. It was revealed as the preference.

Pros:

  • Transformation rules could be referenced separately by other proposal

Cons:

  • Not in line with the wishes of the poll

4.4 Alternative: Use More Imperative And Narrative Language

It was not a long time ago that the law was taken. greater narrative and somewhat more imperative language. This approach was decried by a vocal minority of participants. After some time, PSR-0; She edited his opinion on the text.

Compatability Note with PHP 5.3.2 and below

This is a guideline. Failing to strip the leading namespace seperator could lead to unexpected behavior.

5. People

5.1 Editor

  • Paul M. Jones, Solar / Aura

5.2 Sponsors

  • Phil Sturgeon, PyroCMS (Coordinator)
  • Larry Garfield, Drupal

5.3 Contutors

  • Andreas Hennings
  • Bernhard schussek
  • Beau simensen
  • Donald gilbert
  • Mike van Riel
  • Paul Dragoonis
  • Too many others to name and count

6. Votes

  • Entrance Vote: https://groups.google.com/d/msg/php-fig/_LYBgfcEoFE/ZwFTvVTIl4AJ

  • Acceptance Vote:

    • 1st attempt: https://groups.google.com/forum/#!topic/php-fig/Ua46E344_Ls, presented prior to new workflow; due to accidental modification

    • 2nd attempt: https://groups.google.com/forum/#!topic/php-fig/NWfyAeF7Psk, cancelled at the discretion of the sponsor https://groups.google.com/forum/#!topic/php- fig / t4mW2TQF7iE

    • 3rd attempt: TBD

7. Relevant Links

  • Autoloader, round 4
  • POLL: Autoloader: Split or Combined?
  • PSR-X autoloader spec: Loopholes, ambiguities
  • Autoloader: Combine Proposals?
  • Package-Oriented Autoloader, Round 2
  • Autoloader: looking again at namespace
  • DISCUSSION: Package-Oriented Autoloader - vote against
  • VOTE: Package-Oriented Autoloader
  • Proposal: Package-Oriented Autoloader
  • Towards a Package Oriented Autoloader
  • List of Alternative PSR-4 Proposals
  • Summary of [post-Acceptance Vote pull] PSR-4 discussions

Autoloader

The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119.

1. Overview

This PSR describes the autoloading classes from file paths. It is fully interoperable, and it can be used in addition to any other autoloading specification, including PSR-0. This will be autoloaded according to the specification.

2. Specification

  1. The term "class" refers to classes, interfaces, traits, and other similar structures.

  2. A fully qualified class name has the following form:

     \<NamespaceName>(\<SubNamespaceNames>)*\<ClassName> 
    1. The fully qualified class name has been defined as a "vendor namespace".

    2. MAY have one or more sub-namespace names.

    3. MUST have terminating class name.

    4. Underscores.

    5. MAY BE ANY BATTERY AND ALTERNATIVE.

    6. All class names MUST be referenced in a case-sensitive fashion.

  3. When loading a file that corresponds to a fully qualified class name ...

    1. If you’re looking for a new name, you’ll find out what you’re looking for (or "namespace prefix").

    2. The contiguous sub-namespace names after the "namespace prefix" correspond to the subdirectory within a "base directory", in which the namespace separators are the directory separators. The subdirectory name MUST match the case of the sub-namespace names.

    3. The terminating class name .php . The file name MUST match the case of the terminating class name.

  4. MUST NOT return excerpts, MUST NOT return a value.

3. Examples

The table below shows the corresponding file path for the prefix, and the base directory.

Fully Qualified Class Name Namespace Prefix Base directory Resulting File Path
\ Acme \ Log \ Writer \ File_Writer Acme \ Log \ Writer ./acme-log-writer/lib/ ./acme-log-writer/lib/File_Writer.php
\ Aura \ Web \ Response \ Status Aura \ Web / path / to / aura-web / src / /path/to/aura-web/src/Response/Status.php
\ Symfony \ Core \ Request Symfony \ core ./vendor/Symfony/Core/ ./vendor/Symfony/Core/Request.php
\ Zend \ Acl Zend / usr / includes / Zend / /usr/includes/Zend/Acl.php

For example implementations of autoloaders conforming to the specification, please see the examples file. MAY change at any time.


Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Software and information systems development

Terms: Software and information systems development