Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the development specification of PHP-PSR modern PHPer

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

PHP-PSR modern PHPer development specification is what, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

PSR is the abbreviation of PHP Standards Recommendation, which means PHP recommendation. To understand PSR, you first need to know who the person / organization that sets this standard is-PHP-FIG.

PHP-FIGPHP-FIG, known as PHP Framework Interop Group, is an organization whose members are made up of representatives of the PHP framework who come together to "discuss commonalities between frameworks and find ways to work together." PHP-FIG has developed recommended specifications that can be voluntarily implemented by the PHP framework to improve the communication and sharing capabilities of other frameworks.

The mission of PHP-FIG is to achieve interoperability between frameworks. PSR-1: basic code style

At the beginning of the article, we have briefly introduced what PSR,PSR is the PHP standard, and PSR-1 is the most basic and simple standard of PHP.

The PHP tag must put the code in the or tag. Do not use other tag syntax

It is believed that many PHPer are easy to follow, and normal tags are generally used in real code.

Encoding all PHP files must be encoded using the UTF-8 character set and must not have byte order marks (Byte Order Mark,BOM)

This is also very common, that is, there is no BOM and there is a BOM format. I remember that when I first typed the PHP code, I always took good care of it. I must use IDE to change it to a BOM-free format. I said I was ignorant at that time, and then I did it. Now I see here and re-check the data to find out why the BOM format cannot be used. BOM produces extra output, just like adding a blank line for no reason:

Php sometimes has errors when dealing with BOM headers, which may cause you to output files when using functions such as header or session_start, mostly because BOM headers are sent out. Because in php's view, it becomes a space. So using a BOM-free format, a PHP file can define symbols (classes, properties, functions, constants, etc.), or perform operations with side effects (generating results or processing data), but not two things at the same time

The meaning of this stipulation is almost a variable, a method or a class, which can only complete one operation and do one thing accordingly, which not only ensures the clarity and understandability of the code, but also ensures the oneness of methods and variables. In fact, it is also for convenience, we can decouple very well when the project / application is large in the future.

Namespaces and classes that automatically load PHP must comply with the PSR-4 autoloader standard

Follow up and see the specific explanation of PSR-4.

The name of the class PHP the name of the class must be humped, also known as title

Hump type and participle type (each word is separated by _) these two kinds of writing, remember that there was a great controversy in the past, some people support hump (GirlFriend), some people support participle (girl_friend), now well, unified provisions, for the better development of PHP, then aggrieved to support the brother of participle, unified hump. I remember that the company's CI2 project used this kind of participle, but it was also a framework. Later, in my own project, I had a hunch to use the hump. Haha, I liked my vision.

Constant name PHP constant name must be capitalized

There should be no doubt about this. When I first wrote PHP, it was already deeply ingrained.

The name of the method uses hump (boyFriend)

The naming of a method is similar to that of a class, but there are some differences: class naming requires uppercase (BoyFriendMoney), while method naming requires lowercase (boyFriendMoney).

PSR-2: strict code style

PSR-2 is a stricter code specification than PSR-1. Both individuals and officials believe that developers should follow more stringent code standards. In the modern PHP ecosystem, a unified style can better let other developers understand PHP code.

Implement PSR-1 before using PSR-2 to implement PSR-1 indentation using four space indents.

I believe there is a lot of controversy on the issue of indentation. I always use IDE's tab key until I really face up to this problem. Then when colleagues and themselves are writing the same file, there will be different indentation of the first code, resulting in a very messy code structure. So after this problem, we unify the standard of file indentation, indenting with four spaces. In this way, even if you open it with an inconsistent editor, the effect is the same.

A lot of IDE can be set tab key, Baidu can be found at once.

Files and lines of code PHP files must use UNIX-style newline characters (LF), end with a blank line, and cannot be closed with the PHP? > tag.

At first, I didn't understand why I didn't use the close? > tag in pure PHP pages, but later I found the answer in the book.

To avoid unexpected output errors, if a close tag is added and there is a blank line after closing the tag, then the blank line is also treated as output, resulting in an error (for example, when setting the http header) keyword, using lowercase

I don't know where I read the PHP tutorial before. The keywords written above, such as true and false, use uppercase TRUE and FALSE. I have been using them in this way. Later, when I saw the specification of PSR-2, I knew that I should use lowercase. I was tired.

Namespace each namespace statement must be followed by a blank line. Similarly, when you import a namespace or create an alias for a namespace using the use keyword, add a blank line after a series of use declaration statements

Similar to:

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report