12th Article PSR-1 specification

Source: Internet
Author: User

This specification is not many, seven points are as follows:

1. Overview
    • Files must use only <?php and <?= tags.

    • Files must use is UTF-8 without BOM for PHP code.

    • Files should either declare symbols (classes, functions, constants, etc.) orcause side-effects (e.g. generate output, change. ini settings, etc.) but should does both.

    • Namespaces and classes must follow an "autoloading" PSR: [PSR-0, PSR-4].

    • Class names must is declared in StudlyCaps .

    • Class constants must is declared in all upper case with underscore separators.

    • Method names must is declared in camelCase .

1th article

This is basically what we all know, PHP code must use only long label ( <?php ?> ) or short output type label ( <?= ?> ), and do not use other tags.

This is because we are learning PHP, the general textbook tells us that PHP has 4 kinds of markup style: PHP 4 kinds of marker style so, a lot of people are crazy. The blind Dick wrote. Especially ASP style part-time is torture good!!!

2nd article

This does not say, save the time format must be no BOM UTF-8 format, otherwise there will be a lot of unexplained strange problems. Don't be a jerk. Save the file with the text editor under Windows

3rd article

That's what the layman says. is not to have some output and modify the operation ( 副作用 ) and the class file mixed together, focus on a bit, this file specifically to declare class, that file specifically to modify the configuration file, do not mix together write:

Therefore, the following file is problematic, it is best not to:

// 副作用:修改了ini配置

ini_set(‘error_reporting‘, E_ALL);

// 副作用:载入了文件

include "file.php";

// 副作用:产生了输出

echo ";

// 声明 function

function foo()

{

    // 函数体

}

You see how messy it looks. It's best to write it all apart:

namespace Lib;

class Name

{

    public function __construct()

    {

        echo __NAMESPACE__ . "<br>";

    }

    public static function test()

    {

        echo __NAMESPACE__ . ‘ static function test <br>‘;

}

}

Modify INI:

ini_set(‘error_reporting‘, E_ALL);

Require file:

require DIR . ‘/loading.php‘;

spl_autoload_register("\\AutoLoading\\loading::autoload");

Do you think it's more neat? Of course, this is hard to restrain. themselves carefully divided.

4th article

The 4th one is bound namespace, which has been said before, not much. It's worth saying that the name is the hump way.

5th article

Class name must be written in camel, hump and small hump and big hump (small hump is the first letter is lowercase) so write to look comfortable also more norms, do not require, anyway is the hump on it can be. I like to use a small hump:

class getUserInfo

{

}

6th article

A declaration in a prescribed class 常量名(const) must be 全部大小 , if there are multiple words, _ separated by:

class getUserInfo

{

    //全部大写

    const NAME = ‘phper‘;

    //用_隔开

    const HOUSE_INFO = ‘已经大上海买房‘

    public function getUserName()

{

         //

    }

}

7th article

Method name must be written in camel style, size hump can, do not require, I like to use small hump:

class getUserInfo

{

    public function getUserName()

    {

        //

    }

}

12th Article PSR-1 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.