Home > Backend Development > PHP Tutorial > Master PHP naming rules simply_PHP tutorial

Master PHP naming rules simply_PHP tutorial

WBOY
Release: 2016-07-15 13:28:26
Original
1081 people have browsed it

When you first learn PHP, you may encounter PHP naming problems. Here we will introduce the PHP naming rules. I hope it can help everyone. Careful friends can write it down and keep it as future learning materials.

1. PHP Naming Rules Proper naming

Naming is the core of program planning. The ancients believed that knowing a person's true name would grant them incredible power over that person. As long as you think of the right names for things, it will give you and those who come after you more power than code. Don't laugh! A name is a long-lasting and far-reaching result of a thing in its ecological environment. In general, only programmers who understand the system can come up with the most appropriate name for the system. If all names are naturally appropriate, the relationship is clear, the meaning can be deduced, and ordinary people's inferences can be expected.

If you find that only a few of your names match their corresponding objects, it’s best to take a good look at your design again.

2. PHP Naming Rules Class Naming

Before naming a class, you must first know what it is. If you still can't remember what the class is based on the clues provided by the class name, then your design is not good enough. Mixed names consisting of more than three words can easily cause confusion between various entities in the system. Take another look at your design and try to use (CRC Session card) to see if the entity corresponding to the name has so many functions. When naming a derived class, you should avoid the temptation to include the name of its parent class. The name of a class is only related to itself and has nothing to do with the name of its parent class. Sometimes suffixes are useful. For example, if your system uses an agent, then name a component "DownloadAgent" to actually transmit information.

3. PHP Naming Rules Method and Function Naming

Usually each method and function performs an action, so their naming should clearly describe them What it does: Use CheckForErrors() instead of ErrorCheck(), and use DumpDataToFile() instead of DataFile(). Doing so also makes functions and data more distinguishable objects.

Sometimes suffixes are useful:
◆Max - meaning the maximum value that can be assigned to an entity.
◆Cnt - The current value of a running count variable.
◆Key - key value.

For example: RetryMax represents the maximum number of retries, and RetryCnt represents the current number of retries.

Sometimes prefixes are useful:
◆Is - meaning to ask a question about something. Whenever people see Is they know it's a problem.
◆Get - means to get a value.
◆Set - means setting a value

4. PHP naming rules abbreviations should not use all capital letters

In any case, when you encounter the following situations, you can use the first letter to be capitalized and the remaining letters to be lowercase instead of using all capital letters to represent abbreviations. Use: GetHtmlStatistic. Don't use: GetHTMLStatistic. Reason: People seem to have very different intuitions when names contain acronyms. It is best to have unified regulations, so that the meaning of the naming is completely predictable. Take the example of NetworkABCKey. Note whether C should be the C in ABC or the C in key. This is very confusing. Some people don't care, others hate it. So you will see different rules in different codes, so you don't know what to call it.

For example:

<ol class="dp-xml">
<li class="alt"><span><span>class FluidOz // 不要写成 FluidOZ   </span></span></li>
<li class=""><span>class GetHtmlStatistic // 不要写成 GetHTMLStatistic   </span></li>
</ol>
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446452.htmlTechArticleWhen you first learn PHP, you may encounter PHP naming problems. Here we will introduce the PHP naming rules. I hope it can help you. , careful friends can write it down and keep it as future study materials. 1....
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template