PSR-0 naming standard for PHP

Source: Internet
Author: User
Tags php class php framework rfc

PSR is an abbreviation for proposing a standards recommendation (standard recommendation), initiated by the PHP Framework Interoperability Group (PHP Universal Framework Group, abbreviated as Php-fig), By naming them, this is an open-ended group that is focused on the versatility of the framework and has its own warehouse address on GitHub, and there is only one accepted standard, that is the PSR-0 standard, which defines the naming conventions and file path specifications that PHP automatically loads. The following points are mainly mentioned for the PSR-0 standard:

Requirements
    • A fully qualified namespace and class name must have the following structure "\< Provider name >\ (< namespace >\) *< class name >"
    • Each namespace must have a top-level namespace ("provider")
    • Each namespace can have any number of sub-namespaces
    • Each namespace must be converted to the operating system path delimiter (Directory_separator) when it is loaded from the file system
    • Each "_" character is converted to Directory_separator in the class name. The "_" symbol does not have this meaning in the namespace
    • Namespaces and class names that conform to the naming standard must be loaded with the end of ". PHP"
    • Provider name, namespace, class name can consist of uppercase and lowercase letters, where the namespace and class names are case sensitive to ensure multiple system compatibility
    • If the file does not exist, you need to return false

It's more difficult to understand, look at the following:

1, psr-0 Specification [1] namespace must be consistent with absolute path [2] class name initials must be capitalized [3] Except for the entry file, the other ". PHP" must have only one class [4]php class files must be automatically loaded, not include, etc. [5] Single entry

Example:

\doctrine\common\isolatedclassloader =/path/to/project/lib/vendor/doctrine/common/=/path/to/ project/lib/vendor/symfony/core/=/path/to/project/lib/vendor/zend/=/path/to/ project/lib/vendor/zend/mail/message.php

Extension examples:

Provides a function to show how to use the above criteria.

<?phpfunction AutoLoad ($className) {$className= LTrim ($className,'\\'); $fileName="'; $namespace="'; if($lastNsPos = Strripos ($className,'\\')) {        $namespace= substr ($className,0, $lastNsPos); $className= substr ($className, $lastNsPos +1); $fileName= Str_replace ('\\', Directory_separator, $namespace) .    Directory_separator; } $fileName.= Str_replace ('_', Directory_separator, $className).'. PHP'; Require $fileName;}

The realization of Splclassloader

The next gist implementation of the Splclassloader can load you follow the above standard to implement the General class library, which is 5.3 recommended loading method.

http://gist.github.com/221634

Extended implementations

Because this standard mentions that if the file does not exist, it should be a range of false, but in the above example of the function does not implement the mechanism, all people have achieved the optimization of the Splclassloader.

<?PHPclassclassloader{/** * @var array Contains namespace/class prefix as key and sub path as value*/    protected$paths; /** * Construct a Loader instance * * @param array $paths containing class/namespace prefix as key and sub P Ath as value*/     Publicfunction __construct (array $paths) {$ This->paths =$paths; }    /** * Load classes/interfaces following PSR-0 naming * * @param string $className * @return Null|boolean     Null if no match is found, bool if match and Found/not found. */     Publicfunction Load ($className) {if($className [0] ==='\\') $className= substr ($className,1 ); foreach( $ This->paths as$prefix =$subPath) {            if(Strpos ($className, $prefix)!==0 )                Continue; $lastNsPos= Strripos ($className,'\\' ); $prefixLen= strlen ($prefix) +1; $fileName=$subPath.            Directory_separator; if($lastNsPos >$prefixLen) {                //replacing "to"/' in namespace part$fileName. =Str_replace ('\\', Directory_separator, substr ($className, $prefixLen, $lastNsPos-$prefixLen)) .            Directory_separator; }            //replacing ' _ ' to '/' on ClassName part and append '. php '$fileName. = Str_replace ('_', Directory_separator, substr ($className, $lastNsPos +1) ) .'. PHP'; if(($fileName = Stream_resolve_include_path ($fileName)) = = =false )                return false;            Require $fileName; return true; }    }}

Reference Address: Https://github.com/andrerom/fig-standards/blob/psr2/proposed/PSR-2.md

Write it in the back.

Standards are a good thing for developers, and more and more open-source projects are now joining the standard PEAR2, PHPBB, Composer, Packagist, Joomla, Drupal, Symfony, CakePHP, Doctrine2, etc. One of the frameworks I used a lot of MICROMVC also used this standard very early on. Projects with the same standards can be seamlessly accessed, making it best for developers to try and receive a good standard.

Extended Reading

PHP official about Splclassloader Rfc:https://wiki.php.net/rfc/splclassloader PHP Standardization Organization forum: https://groups.google.com/forum/? Fromgroups#!forum/php-standards

Original: http://www.4wei.cn/archives/1002186

Another article: HTTP://WWW.TUICOOL.COM/ARTICLES/ZJ2MFA

PSR-0 naming standard for PHP

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.