11th Article PSR-0 specification

Source: Internet
Author: User

Mandatory

    • A fully-qualified namespace and class must have the following structure\<Vendor Name>\(<Namespace>\)*<Class Name>
    • Each namespace must has a top-level namespace ("Vendor Name").
    • Each namespace can has as many sub-namespaces as it wishes.
    • Each namespace separator are converted to a when loading from the DIRECTORY_SEPARATOR file system.
    • Each of the character in the _ CLASS NAME was converted to a DIRECTORY_SEPARATOR . The _ character has no special meaning in the namespace.
    • The Fully-qualified namespace and class is suffixed with when loading from the .php file system.
    • Alphabetic characters in vendor names, namespaces, and class names could be of any combination of lower case and upper case.
Examples
    • \Doctrine\Common\IsolatedClassLoader=/path/to/project/lib/vendor/Doctrine/Common/IsolatedClassLoader.php
    • \Symfony\Core\Request=/path/to/project/lib/vendor/Symfony/Core/Request.php
    • \Zend\Acl=/path/to/project/lib/vendor/Zend/Acl.php
    • \Zend\Mail\Message=/path/to/project/lib/vendor/Zend/Mail/Message.php
Underscores in namespaces and Class Names
    • \namespace\package\Class_Name=/path/to/project/lib/vendor/namespace/package/Class/Name.php
    • \namespace\package_name\Class_Name=/path/to/project/lib/vendor/namespace/package_name/Class/Name.php

The standards we set here should is the lowest common denominator for painless autoloader interoperability. You can test, that's following these standards by utilizing this sample Splclassloader implementation which is able t O Load PHP 5.3 classes.

This is the first standard of PHP, E, if the E-text good, you can see the original, bad listen to my nagging nagging, the wrong place, please criticize point, thank you! The main is to develop a number of automatic loading standards (autoloading standard), very short. This standard was obsolete in 2014 and replaced by the new PSR-4 standard. But we can also look at it, this does not affect our study.

PSR-0 Mandatory Requirements The following points:

    1. A fully qualified Namespace and class must conform to this structure: "\< Vendor name> (< namespace>) *< Class name>"
    2. Each namespace must have a top-level namespace ("Vendor name" provider name)
    3. Each namespace can have multiple sub-namespace
    4. When loading from the file system, each namespace delimiter (/) is converted to DIRECTORY_SEPARATOR (operating system path delimiter)
    5. In the class name, each underscore (_) symbol is converted to DIRECTORY_SEPARATOR (the operating system path delimiter). In namespace, the underscore (_) symbol has no (special) meaning.
    6. When loaded from the file system, the qualified namespace and class must end in. php.
    7. Verdor name,namespaces,class names can be combined in uppercase and lowercase letters (case sensitive)

The main talk here is namespace and autoloading.

1th article

such as my file such /Doctrine/Common/IsolatedClassLoader.php a class file, then your namespace name must declare that:

声明:namespace  \Doctrine\Common调用:\Doctrine\Common\IsolatedClassLoader

Where Doctrine represents a module directory Vendor name, Common is Namesapce, Isolatedclassloader is class name. Such a look will know the directory hierarchy of this file, at a glance.

Another example:/path/to/project/lib/vendor/symfony/core/request.php file:

声明:namespace \Symfony\Core调用:\Symfony\Core\Request
2nd, Article 3
namespace \Zend\Acl => /path/to/project/lib/vendor/Zend/Acl.phpnamespace \Zend\Mail\Message => /path/to/project/lib/vendor/Zend/Mail/Message.php

There must be a top-level Zend namespace, Zend The following can have a message child namespace.

4th article

Look at this example, we need a new class like this.

new \Symfony\Core\Request

So when I load this class file again, I'm going to convert the delimiter \ into a directory, that is, go to Vendor----symfony->core->request.php layer of directory to find this file. In fact, the corresponding relationship with the first is the opposite.

5th article

5th is said that the namespace name in the _ symbol is not any use, is used to represent the directory delimiter, but notice in PRS-4 has canceled this _ , then we still look at, this outdated rule is how:

\namespace\package\Class_Name => /path/to/project/lib/vendor/namespace/package/Class/Name.php\namespace\package_name\Class_Name => /path/to/project/lib/vendor/namespace/package_name/Class/Name.php

The _ in the above 2 namespace is actually a directory separator. That's not what class name is. Now, after PSR-4 out, I do feel that such a rule is a little bad. It's weird.

6th article

6th don't say much. Since you use PHP, the filename is, of course, the end of the. php suffix. The reason for this rule, I guess roughly, is someone using the. php3 suffix. All right. This is a myth. Because it is allowed in the Apache configuration file:

<IfModule dir_module>    DirectoryIndex index.php index.php3 index.html index.htm</IfModule>

So, be honest with. php as the suffix name.

7th article

File capitalization problem, this is actually very important. Because the Linux system is distinguished by file name and directory name capitalization, it is not differentiated under Windows. So there are often problems, such as:

namespace  \Doctrine\Common\IsolatedClassLoader

Under Linux, go to the directories and files strictly according to the size. But if you develop under Windows, all lowercase will not error, you publish to Linux on the tragedy, prompted to find the file. So, capitalization is too important.

11th Article PSR-0 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.