Home > Backend Development > PHP Tutorial > Naming rules that PHP programmers are accustomed to

Naming rules that PHP programmers are accustomed to

黄舟
Release: 2023-03-14 13:20:02
Original
3458 people have browsed it

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.

1. File name

File names use camel case naming method, that is, capitalize the first letters of all words, such as: UserLoginController.php

2. Class name

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.

3. Class method name

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

4. Class member variables and method variables

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

5. Class constants, global constants

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!

Related labels:
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