Home  >  Article  >  Backend Development  >  What is PHP PSR?

What is PHP PSR?

Guanhui
GuanhuiOriginal
2020-06-28 11:59:465663browse

PHP PSR is a PHP specification developed by the PHP-FIG organization, which is a practical standard for PHP development. The PHP-FIG organization was established in 2009 by several open source framework developers, although it is not an "official" organization. , but also represents the majority of the PHP community.

What is PHP PSR?

##PSR-1: Basic coding style

At the beginning of the article, We have already briefly introduced what PSR is. PSR is a PHP standard, and PSR-1 is the most basic and simplest standard for PHP.

PHP tag

The code must be placed in the or tag. Do not use other tag syntax

I believe many PHPers are easy to comply with this, and in real code, normal tags

## are generally used #Encoding

All PHP files must use UTF-8 character set encoding, and cannot have Byte Order Mark (Byte Order Mark, BOM)

This is also very common, that is BOM-free and BOM-based formats. I remember when I first started typing PHP code, my seniors were always very concerned about it, and I had to use the IDE to adjust it to the BOM-free format. I was confused at the time, and then I followed suit. Now that I see this, I started to do it again. After checking the information, I found the reason why the BOM format cannot be used. The BOM will produce redundant output, just like an extra blank line for no reason:

When php processes the BOM header, there are sometimes errors. , which may cause an error that the file has been output when you use functions such as header or session_start. Most of the time it is because the BOM header is sent out. . Because in php's view, it becomes a space. So use the BOM-free format

Purpose

A PHP file can define symbols (classes, traits, functions, constants, etc.), or perform operations with side effects (generate result or process data), but cannot do two things at the same time

This rule basically means that a variable, method or class can only complete one operation and do one thing accordingly, thus ensuring the code It is clear and easy to understand, and it also ensures that the methods and variables are single and each performs its duties. In fact, it is also for convenience. When the project/application becomes larger in the future, we can decouple it well

Automatic loading

PHP namespaces and classes must comply with PSR -4 Autoloader standards

The name of the class

The name of the PHP class must use camel case, also known as title style

Camel case and word segmentation These two writing methods (each word is separated by _), I remember there was a lot of controversy in the past. Some people supported the camel case (GirlFriend), and some supported the word participle (girl_friend). Now it’s better, and they have been unified. In order to make PHP more If the development is good, then we will reluctantly support the participle-style brothers and unify the hump. I remember that the company's CI2 project used this kind of word segmentation, but it was also a framework requirement. Later, in my own project, I had a hunch and used the camel case, haha, thumbs up for my vision~

Names of constants

PHP constant names must be capitalized;

There should be no doubt about this. When I first started writing PHP, this writing method was already ingrained.

Method name

Use camel case (boyFriend)

The naming of methods is somewhat similar to the naming of classes, but there are still some differences: class The naming of methods requires the first letter to be capitalized (BoyFriendMoney), while the naming of methods requires the first letter to be lowercase (boyFriendMoney)

Recommended tutorial: "
PHP

"

The above is the detailed content of What is PHP PSR?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn