Talking about PHP code specification

Source: Internet
Author: User
Tags pear php framework codeigniter zend framework

Learn a little bit of programming every day PDF ebook, video tutorial free download:
Http://www.shitanlife.com/code

Objective

Although PHP is the best language in the world, the PHP code written is often not the most beautiful. The reason, may be formally because PHP simple easy to use, suitable for fast iterative characteristics, resulting in our immersion in the rapid completion of the needs of the iteration of the theft, but forget the normative, ignoring the ease of maintenance, to the posterity dug countless pits, behind the maintenance of simply want to dozens. Phper may I ask you if you have written the following code?

"1" a function that writes 200 lines or more

"2" a function has seven or eight or even more than 10 parameters.

"3" Single line code/string maximum of 120 characters

"4" a PHP file wrote thousands of lines

"5" Changes the code without changing the corresponding comment

"6" does not use the package provided by the web framework, but instead uses $_post, $_get, $_session these global variables

"7" ...

In fact, all of the above problems really exist in our project. Writing the code above does not affect the functionality of the code, but the so-called walnuts posterity, messy code like a bunch of weeds, the future of the maintenance of a lot of weeds than a tree more pain. It is difficult to quantify the efficiency losses that result. Imagine reading a 500-line function, where the local variables are defined by no more than 50, you see a variable, the mind is not expected to the meaning of this variable, but also go back to find the definition of its place, step by step tracking down perhaps the idea has been interrupted. If you read a 50-line function, the entire function body can be accommodated in a computer screen, even if the mouse does not have to flip to see all, then the heart will be how comfortable.

Some examples of the opposite

"1" Endless function parameters

"2" wrote more than 20,000 lines of code files

"3" hand-SQL statement, "seemingly" very convenient

"4" six-layer foreach nesting

Make changes

Once you've seen many development groups and become aware of code specification issues, you'll develop your own code specifications. Once we also wanted all the development to sit down and negotiate a uniform code specification in a friendly way. However, the first is to take time, the second is not enough detail, the discussion is difficult to relate to all aspects of coding, the third and most important point, can not reach agreement ... Presumably everyone has heard of an enduring joke in the programming world, that is, whether the encoding should be indented with a space or with a tab indent. Just code specification of this kind of thing, it is no standard answer, you can list 10 of the advantages of using the space to indent, but immediately someone will put forward 10 use tab to do the benefits of indentation. The "discussion" approach simply won't work.

To be able to specify and follow a "standard" without an answer, the discussion is not going to produce results, and what we need at this time may be "authority" and "power". "authority" means to find out what the industry Daniel is doing, and it is not wrong to take their practice as our "standard". "Power" refers to the determination of the "standard", relying on the development of leader to push the group to all people, there is no room for discussion. "authority" guarantees the correctness of code specifications, and "power" guarantees the execution of code specifications.

Who is the "authority"?

With the above ideas, we will discuss whose code specification can represent "authority". Usually use PHP to do web development, presumably everyone will use a variety of PHP framework, such as Laravel,symfony,yii framework,zend framework and so on. As the world-renowned open source framework, the code in these frameworks should be very well-regulated. So what do they fit into the norm?

In fact, you can find a random search, most of the framework follows the PSR (PHP standard recommendation) specification. PSR is a norm developed by a Democratic vote by an organization called PHP-FIG (Framework Interoperability Group). At a PHP technology conference in 2009, the authors of each PHP framework spontaneously set up the Php-fig organization, the main purpose of which is to provide the framework authors with some excellent design specifications and to be consistent with each framework. So PSR actually represents the framework's design specifications, including but not just code writing specifications.

The PSR specification currently has more than 10 specifications, from PSR1, PSR2 、......、 PSR17 (also in the extension), covering all aspects of the PHP web framework design. And what we are most concerned about is PSR1 and PSR2, the two specifications described in the formal PHP code. If you are interested, you can go to their official website to see (http://www.php-fig.org/), including the current specifications, which are being developed, the introduction of the organization and so on.

The vast majority of PHP Web frameworks now follow PSR2 code specifications.

What are the PHP code specifications besides PSR?

When I see the influence of the PSR specification, I have basically decided to use it as the coding norm in our project. But in a scientific manner, we still need to know what other norms the industry has.

If you're a PHP veteran, you might have used pear to manage PHP libraries (although Pear has been completely covered by composer). Pear also has its own code specification (link: https://pear.php.net/manual/en/standards.php), pear specification should belong to the earliest specification of PHP, and many other code specifications have evolved from it. Its requirements are also the most lenient.

If you've ever heard of PHP code Sniffer (https://github.com/squizlabs/PHP_CodeSniffer), PHP codes Check and Beautify tool, you can see the MySource, Squiz, PHPCS specification. PHP code Sniffer is an open source tool called Squiz, so be sure to recommend a code specification that your company uses, the Squiz specification, which is much stricter than PSR. For example, the following notation does not conform to the specification in PSR2, but is not standard in Squiz. Squiz stipulates that when a class needs to refer to itself, it must not use the class name, but use self.

12345678910111213 classTest {     public function getInstance()     {         return new Test(); // 符合PSR,但不符合Squiz规范,Squiz规范要求这里写为 new self();     }}

  

Phpcs is the code used by the sniffer tool itself, slightly looser than squiz, but still much stricter than PSR. The MySource specification is the strictest one for the company's MySource Mini project, which has not only PHP specifications, but also JS and CSS specifications. A tool has provided three of its own company's specifications ...

Briefly summarize:

Degree of rigor PEAR < PSR < Phpcs < Squiz < MySource

Prevalence of PSR > PEAR > Others

In fact, there are also some PHP frameworks that provide specifications for their own frameworks, such as CodeIgniter (http://codeigniter.org.cn/user_guide/general/styleguide.html). It is strange why this framework did not join the php-fig, but such frameworks are generally relatively small in their own definition specifications. If you use CodeIgniter, it is certainly advisable to follow the framework's own specifications. This is no longer a detailed study.

According to PHP Code sniffer author's recommendation, if you do not know which good to use, then first try to use PSR2 bar!

How to act?

In fact, in the course of the implementation of the standard, we must encounter a lot of difficulties at the beginning. For example, their own code writing habits and norms are not the same, just a change of time will be somewhat awkward, often dozens of pages of the standard content, can not remember, how to do? PSR2 's code specification links are as follows: http://www.php-fig.org/psr/psr-2/, it seems that the scroll bar is very short, the page content is too much, in fact, they can be compressed on a piece of paper. In the Group project development, the PSR2 in Chinese a little translation (express the same meaning, the Chinese sentence is much shorter than English), reduce some of the font size, put on a piece of paper printed out, posted on the desk, a long time natural can develop a good habit. This method has been implemented for a period of time in the project team, the effect is gratifying.

Conclusion

Wrote a long-length PHP code specification, but did not appear any specifications of the detailed content. Mainly because the general in the blog written in the long-winded specification seems that we do not like to see ~ as a "ramble", just a record of their own search for a "scientific" code specification process. The content of the specification may not be consistent with your usual habits, but this is a consistent choice for each PHP web framework developer.

Learn a little bit of programming every day PDF ebook, video tutorial free download:
Http://www.shitanlife.com/code

Talking about 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.