» » PHP PSR-4 Autoloader

 

PHP PSR-4 Autoloader

Author: bamboo06 on 15-02-2019, 00:56, views: 392

0
Prior to PSR-4, PHP components and frameworks used the magic method _autoload() or spl_autoload_register() to implement their own autoloaders, so we learned to use each component and framework's unique autoloader, and Nowadays, everyone follows PSR-4, which means we can mix and match multiple PHP components with just one autoloader.
PHP PSR-4 Autoloader

The PHP framework works with an automatic loading mechanism. Autoloading is the process by which the PHP interpreter automatically finds and loads PHP classes as needed at runtime. This is an amazing feature that links many interoperable components of the modern PHP ecosystem.

1 Overview
This PSR is related to the automatic loading of the corresponding class by the file path. This specification is interoperable and can be used as a supplement to any autoloading specification, including PSR-0. In addition, this PSR also includes autoloading. The file corresponding to the class stores the path specification.

2. Detailed description
The "class" here refers to all class classes, interfaces, traits reusable code blocks, and other similar structures.

A complete class name needs to have the following structure:
\<namespace>(\<sub-namespace>)*\<class name>


1). The complete class name must have a top-level namespace, called "vendor namespace";

2). The complete class name can have one or more sub-namespaces;

3). The complete class name must have a final class name;

4). The down line in any part of the complete class name has no special meaning;

5). The complete class name can be composed of any uppercase and lowercase letters;

6). All class names must be case sensitive.

When loading the corresponding file according to the full class name

1). In the complete class name, remove the first namespace separator, one or more consecutive namespaces and sub-namespaces, as the "namespace prefix", which must correspond to at least one "file base directory" ;

2). The sub-namespace immediately after the namespace prefix must match the corresponding "file base directory", where the namespace separator will be used as the directory separator.

3). The class name at the end must be the same name as the corresponding file suffixed with .php.

4). The implementation of the autoloader must not throw an exception, must not trigger any level of error information and should not have a return value.

3. Examples
The following table shows the file paths that correspond to the specification full class name, namespace prefix, and file base directory.

Example 1:
Full category name: \Acme\Log\Writer\File_Writer
Namespace pre-name: Acme\Log\Writer
Base directory: ./acme-log-writer/lib/
File path: ./acme-log-writer/lib/File_Writer.php

Example 2:
Full category name: \Aura\Web\Response\Status
Namespace pre-name: Aura\Web
Base directory: /path/to/aura-web/src/
File path: /path/to/aura-web/src/Response/Status.php

Example 3:
Full category name: \Symfony\Core\Request
Namespace pre-name: Symfony\Core
Base directory: ./vendor/Symfony/Core/
File path: ./vendor/Symfony/Core/Request.php

Example 4:
Full Category Name: \Zend\Acl
Namespace pre-name: Zend
Base directory: /usr/includes/Zend/
File path: /usr/includes/Zend/Acl.php

The rule is,
Because: namespace preamble = base directory
So: (full category name - namespace pre-name) = (archive path - base directory)
Also: full category name name after the last delimiter = file path name after the last delimiter.php = category name.

Category: PHP Scripts

Dear visitor, you are browsing our website as Guest.
We strongly recommend you to register and login to view hidden contents.
Information
Comment on the news site is possible only within (days) days from the date of publication.