PSR-0 Automatic loading Standard

Source: Internet
Author: User
Tags autoload autoloader

From 2014-10-21 onwards, the PSR-0 standard was marked as obsolete. It is recommended to use the PSR-4 standard as replacement.

The loader (autoloader) must comply with the requirements below.

Requirements:

    • A complete namespace or class must have the following structure\ \( \)*
    • Each namespace must have a top-level space name (' Vendor name ')
    • Each namespace can have multiple sub-namespaces
    • When loaded from the file system, separators (\) in the namespace are converted to file separators
    • Each of the class names _ (underscores) is converted to a file delimiter. _there is no special meaning in the namespaces
    • When loading from the file system, the full namespace or class must be .php used as the suffix name
    • Developer names, namespaces, and classes can consist of uppercase and lowercase letters (case-sensitive)

      Example

    • \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 classes

    • \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

These should be the minimum standards to implement an autoloader. You can use this to load the PHP 5.3 class of autoloader to practice these standards.

Code implementation


    functionautoload($className){$className= LTrim ($className,'\\');//Remove backslash in front of namespace$fileName='';$namespace='';if($lastNsPos= Strrpos ($className,'\\')) {//The position of the backslash before the class name$namespace= substr ($className,0,$lastNsPos);//Namespace$className= substr ($className,$lastNsPos+1);//class name$fileName= Str_replace ('\\', Directory_separator,$namespace). Directory_separator;//File name}$fileName. = Str_replace ('_', Directory_separator,$className).'. php ';//Standard to be suffixed with. PHP, the underscore of the class name needs to be converted to a file delimiterrequire$fileName;} Spl_autoload_register (' AutoLoad ');

Splclassloader implementation

Here is a simple splclassloader implementation that you can use to load classes if you follow the above criteria. We recommend using the PSR-0 standard to load PHP 5.3 classes

    • http://gist.github.com/221634 (to turn over the wall??? )

Original Reading PSR-0

'). addclass (' pre-numbering '). Hide (); $ (this). addclass (' has-numbering '). Parent (). append ($numbering); for (i = 1; i <= lines; i++) {$numbering. Append ($ ('
  • '). Text (i)); }; $numbering. FadeIn (1700); }); });

    The above describes the PSR-0 automatic loading standards, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.