Home > Article > Backend Development > Example analysis of PHP code and file naming conventions
During the development process, you should try to follow the following naming convention:
Class files are all suffixed with .class.php (here refers to The class library file used internally by ThinkPHP does not represent the class library file loaded externally), use camel case naming, and the first letter is capitalized, such as DbMysql.class.php
;
The namespace address of the class is consistent with the path address where it is located. For example, Home\Controller\UserController
The path where the class is located should be Application/ Home/Controller/UserController.class.php
;
Ensure that the file naming and calling case are consistent, because on Unix-like systems, the Case is sensitive (ThinkPHP, in debug mode, will strictly check case even on the Windows platform);
The class name and the file name are consistent (including the case mentioned above) ), for example, the file name of the UserController
class is UserController.class.php
, the file name of the InfoModel class is InfoModel.class.php
, and are different There are certain standards for class naming in class libraries;
functions, configuration files and other class library files are generally named .php
Suffix (not required if introduced by a third party);
Use lowercase letters and underscores when naming functions, such as get_client_ip
;
Methods are named using camel case, and the first letter is lowercase or the underscore "_" is used, such as getUserName
, _parseType
, usually Methods starting with an underscore are private methods;
attributes are named using camel case, and the first letter is lowercase or an underscore "_" is used, such as tableName
, _instance
, usually attributes starting with an underscore are private attributes;
Functions or methods starting with a double underscore "__" are used as magic methods. For example __call
and __autoload
;
HAS_ONE and
MANY_TO_MANY;
HTML_CACHE_ON;
MY_LANG, language variables starting with an underscore Usually used for system language variables, such as
_CLASS_NOT_EXIST_;
.html as the suffix (can be modified through configuration);
think_user table and user_name
The fields are written correctly. Data table fields like
_username may be filtered.
Process Statement Specification
1. Branch statement
if($age >= 18 && $age <= 30) { echo 'young man'; } else if($age > 30 && $age <= 60) { echo 'middle aged'; } else { echo 'old man'; } //下面这段代码高手我们一个问题,在if语句中,即使在可以不要花括号的情况下,花括号也是要写上的 if($age > 60) { echo 'I am very old'; } switch($status) { case 'forbiden': echo 'login forbidden'; break; case 'normal': echo 'login in'; break; default: echo 'status is wrong' : break; }
2. Loop statement
while($condition) { statesments......; } foreach($arrayList as $arrayKey => $arrayItem) { states......; } do { statements......; } while($condition) for($start; condition; changenumber) { statements......; }General rules must be followed
The above is the detailed content of Example analysis of PHP code and file naming conventions. For more information, please follow other related articles on the PHP Chinese website!