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.
- A fully qualified Namespace and class must conform to this structure: "\< Vendor name> (< namespace>) *< Class name>"
- Each namespace must have a top-level namespace ("Vendor name" provider name)
- Each namespace can have multiple sub-namespace
- When loading from the file system, each namespace delimiter (/) is converted to
DIRECTORY_SEPARATOR
(operating system path delimiter)
- 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.
- When loaded from the file system, the qualified namespace and class must end in. php.
- 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.