Home > Article > Backend Development > Naming rules that PHP programmers are accustomed to
It has been a long time since I switched from C++ to PHP. I have always been a bit obsessed with code cleanliness. I use strict camel case naming for file names, interface names, class names, method names, etc.
But sometimes I hesitate whether to capitalize the first letter or to use underline to distinguish. Let’s briefly summarize and summarize today.
File names use camel case naming method, that is, capitalize the first letters of all words, such as: UserLoginController.php
Class names also use camel case nomenclature, and the length should be controlled within five words. If the logic is well described, use an abbreviated form, such as: class Account , class RealNameVerify.
The class method name uses camel case naming method, that is, the first word is all lowercase, and the first letter of the subsequent words is all capitalized, and the first letter is as descriptive as possible Operation type, such as: getUserInfo, updateGameConfig, bindPhone.
For private methods of the class, they should start with a single underscore, such as: _guestLogin
Although many people believe that class member variables start with m, it is my personal habit to define all variables in the form of lowercase letters and underlined links, such as: $game_repository, $table_config, $return_data.
By the way Let’s take a look at a better way to name variables:
1. Member variables start with m, such as: $mTable, $mUrl
2. Static variables start with s, such as: $sCount, $ sStatus
3. Global variables start with g, such as: $gConnectTime
For constants, use all uppercase letters and connect words with underscores Method, such as: PAYMENT_METHOD_TTBANK.
Finally, there is no absolute best naming convention, only the naming convention that is most suitable for the team, in detail The naming rules also need to be changed according to the framework used, the specific project background, etc.
But a unified, clear, and simple naming rule is very useful for project development and maintenance.
The above is the detailed content of Naming rules that PHP programmers are accustomed to. For more information, please follow other related articles on the PHP Chinese website!