PHP Daniel Share: PHP code to write the specification, summed up a very full

Source: Internet
Author: User
Tags autoloader php framework
This article mainly introduces about PHP Daniel Share: PHP code to write the specification, summed up a very full, has a certain reference value, now share to everyone, the need for friends can refer to

PHP has always been the most frequently used programming language in web development, and because of this, many practitioners have led to a lot of nonstandard code.

Php-fig (PHP Framework Interface Organization) has developed a complete set of standards, recommended for the vast number of PHP development use.

Altogether, five sets of standards were established:

(psr:php recommended standard)

PSR-1: Basic code style;

PSR-2: strict code style;

PSR-3: Log Logger interface;

PSR-4: Auto Load

In fact, there is a PSR-0, but has been replaced by PSR-4, so there is no PSR-0 version.

First, PSR-1: Basic code style

1. Tags: the PHP code must be written in <?php and, or <?= and?> tags, may not use other formats of the label;

2. Encoding: Must adopt the UTF-8 character set without BOM header, most of the IDE editor is automatically supported;

3. Class Name: Must be used camel: CamelCase, this format is also called title, for example: Indexcontroller;

4. Constants: Uppercase letters must be used, the connection between multiple words is underlined: app_path;

5. Method: Must adopt the small hump type: CamelCase (), for example: GetStatus ();

Second, PSR-2: strict code style

1. The code must first conform to the PSR-1 specification;

2. Indentation: Four spaces must be used uniformly;

3. Line break: The Unix line-wrapping style must be used;

4. End: Must have a blank line, and do not allow the closure of the label?>;

5. No more than 80 characters per line of code, up to a maximum of 120 characters;

6. Keywords all use lowercase letters, for example: True,false,use ...;

7. Namespaces: The following must be followed by a blank line;

8. When you import space, you must immediately follow a blank line;

9. The starting bracket of the class {, the line must be another row;

10. Method and function of the starting parenthesis {, also must be another line;

11. All members of the class must declare visibility: public, protected,private;

12. Characteristics of members in a class: abstract, final, must precede the visibility declaration;

The static keyword, which must be placed after the declaration of the visibility of the class into;

14. The starting parenthesis of the control structure must be on the same line as the statement, for example: if () {};

15. Between the parameters of the control structure, a comma must be followed by a space, for example: ($m, $n);

Third, PSR-3: Log Record interface

This specification differs from the previous specification, it is not a recommendation, but an interface standard, which rules how the logger can be implemented.

As long as you follow this standard, you must implement the following 9 methods:

<?phpnamespace psr\log;interface loggerinterface{public function Emergency ($message, array $context =[]);p ublic function alert ($message, array $context =[]);p ublic function Critical ($message, array $context =[]);p ublic function error ($message, array $context =[]);p ublic function Warning ($message, array $context =[]);p ublic function notice ($message, Array $context =[]);p ublic function info ($message, array $context =[]);p ublic function Debug ($message, array $context =[]) ;p ublic function log ($level, $message, array $context =[]);}

Iv. PSR-4: Auto Loader

1. Why do I have an autoloader?

In a previous PHP script, a large number of files might be loaded:

<?php include ' demo1.php '; include ' demo2.php '; include ' demo3.php ';

With the autoloader, it can be loaded on demand according to the function.

We can register the loader with __autoload () and Spl_autoload_register () before the standard is available, and can now be automatically loaded with namespaces.

2. Automatic loading principle

The main is the class, interface, trait, such as the file path, and the code namespace mapping, so that one by one, given the namespace second Life.

For example:

<?phpnamespace app\controller;class usercontroller{//Code}

Description

1. Class name: App\controller\usercontroller;

2. class file has the same name as the class: app/controller/usercontroller.php

3. The class name and the class file name are mapped through the namespace:

<?phpdefine (' Root_path ', __dir__); Spl_autoload_register (function ($className) {require root_path. '/' . Str_replace (' \ \ ', '/', $className). '. php ';});

4. Associating the class name with the namespace is the foundation of the modern PHP development framework, and composer is also based on this implementation of the component automatic loading;

More programming specifications can be found in the PHP Chinese web (www.php.cn).

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.