15th Article PSR-04 Specification

Source: Internet
Author: User
Tags autoloader

1. Overview

This PSR describes a specification for autoloading classes from file paths. It is fully interoperable, and can being used in addition to any other autoloading specification, including PSR-0. This PSR also describes, where to place files, that'll be is 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 must has a top-level namespace name, also known as a "vendor namespace".
    2. the fully qualified class name may be one or more sub-namespace names.
    3. the fully qualified class name must has a terminating class name.
    4. underscores has no special meaning in any portion of the fully qualified class name.
    5. alphabetic characters in the fully qualified class name is any combination of lower case and upper case.
    6. The
    7. all class names must is referenced in a case-sensitive fashion.
  3. When loading a file, corresponds to a fully qualified class name ...

    1. A contiguous series of one or more leading namespace and sub-namespace names, not including the leading namespace Separato R, in the fully qualified class name (a "namespace prefix") corresponds to at least one "base directory".
    2. The contiguous sub-namespace names after the ' namespace prefix ' correspond to a subdirectory within a ' base directory ' in which the namespace separators represent directory separators. The subdirectory name must match the case of the sub-namespace names.
    3. The terminating class name corresponds to a file name ending in .php . The file name must match the case of the terminating class name.
  4. Autoloader implementations must not the throw exceptions, must not raise errors of any, and should not return a value.

3. Examples

The table below shows the corresponding file path for a given fully qualified class name, namespace prefix, and base Direc Tory.

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. Example implementations must not being regarded as part of the specification and could change at any time.

This specification is an upgrade to the PSR-0 specification,

Here's the difference between PSR-0 and PSR-04,

1) The PSR-04 directory structure is more concise.

2) Note that PSR-0 in the underscore (_) is a special processing, the underscore will be converted to Directory_separator, this is due to the PHP5.3 previous version compatibility considerations, and PSR-4 is not the processing, which is a big difference between the two.

3) PSR-4 requires that the autoloader not be allowed to throw exceptions and raise any level of errors, nor should there be a return value. This is because multiple autoloaders may be registered, and if a autoloader does not find the corresponding class, it should be handed over to the next one instead of blocking the channel.

15th Article PSR-04 Specification

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.