PSR-1 of PHP Code specification

Source: Internet
Author: User

1. Overview
1. 必须使用<?php ?> 或是<?=?>这两种标签2. PHP代码中必须使用UTF-8 without BOM 编码方式3. 每个文件建议只用来声明(类class,函数function,常量constant等)或者只用来做一些辅助操作(输出信息,修改配置等),但一个文件不建议同时做这两件事4. 命名空间Namespace和类class必须遵循"autoloading"PSR标准【PSR-0,PSR-4】5. 类名(class)必须使用大驼峰命名法,如UserInfoController6. 类中的常量必须只能用大写字母和来命名7. 方法名(method)必须使用驼峰命名法,如getUserInfo
2. File 2.1PHP Tag

PHP code must use <?php?> or <?=?> both tags

2.2 Character encoding

PHP code must be encoded using UTF-8 without BOM

2.3 Contents of the file

The rule suggests that a file should have only one behavior, such as the definition of the interface to do the method, the specific function implemented in the implementation class, and the recommendation not to write the interface and implementation in the same file

  <?php        class Fruit            {             public function __construct($type)                {                    $this->_type = $type;                }            }            $fruit=new Fruit(‘pinguo‘);    ?>

This is life and the use of mixed writing, the rules do not recommend this writing, and know the life and use in two files
File 1:

  <?php        class Fruit            {             public function __construct($type)                {                    $this->_type = $type;                }            }    ?>

File 2:

<?php     $fruit=new Fruit(‘pinguo‘);?>

Other, and so on, the goal of the sub-rule is to try to single the function of the class and the file, minimizing the coupling, but this also causes the problem of class and file explosion, which requires balancing and considering the specific project.

3, namespace namespace and class name class ' name '

Namespaces Namespace and Class classes must follow the "autoloading" PSR standard: [PSR-0, PSR-4].

This means that there can be only one class in each file, and at least one level of namespace for each class namespace: That is, a top-level vendor name

The class name must use the large hump nomenclature, such as StudlyCaps

PHP5.3 must be used after the formal namespace namespace, for example:

<?php// PHP 5.3 and later:namespace Vendor\Model;class Foo{}

php5.2.x and previous versions suggest using pseudo-namespace vendor_ as the prefix for the class name

<?php// PHP 5.2.x 及之前:class Vendor_Model_Foo{}
4, class of constant constant, property, method

Class classes refer to all class classes, interface interface, and attributes trait

4.1. Constant constant

Constants in a class must be named only in uppercase letters and _, for example:
<?php

namespace Vendor\Model;class Foo{    const VERSION = ‘1.0‘;    const DATE_APPROVED = ‘2012-06-01‘;}
4.2. Properties

The class's property names can follow the camel-like (StudlyCaps), which begins with uppercase, and should be consistent within a certain range. This range can be the entire team, the entire package, the entire class, or the entire method.

4.3. Methods

The method name must conform to the CamelCase ()-style lowercase start hump naming specification.

References from:

https://www.php-fig.org/psr/psr-1/

PSR-1 of PHP Code 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.