Naming rules
The following naming rules should be observed during thinkphp development:
- All class files are. class. PHP is a suffix (this refers to the class library file used internally by thinkphp, which does not represent the class library file loaded externally). It is named by the hump method and capitalized, for example
DbMysql.class.php
;
- The namespace address of the class is the same as the path address, for example
Home\Controller\UserController
The path of the class should beApplication/Home/Controller/UserController.class.php
;
- Make sure that the file name is the same as the call case, because it is case sensitive on Unix-like systems (while thinkphp is in the debugging mode, even on Windows platforms, the Case sensitivity is strictly checked );
- The class name and file name are the same (including the case-insensitive), for example
UserController
Class file name isUserController.class.php
The file name of the infomodel class isInfoModel.class.php
And the class names of different class libraries have certain specifications;
- Functions, configuration files, and other class library files
.php
Suffix (not required by a third party );
- The name of a function uses lowercase letters and underscores, for example
get_client_ip
;
- The method name uses the camper method, and the first letter is lowercase or the underline "_", for example
getUserName
,_parseType
Generally, the method starting with an underscore is a private method;
- The attribute name uses the camper method, and the first letter is lowercase or the underline "_", for example
tableName
,_instance
Generally, attributes starting with an underscore are private attributes;
- A function or method with the double underscore "_" as the magic method, for example
__call
And__autoload
;
- Constants are named with uppercase letters and underscores, for example
HAS_ONE
AndMANY_TO_MANY
;
- The configuration parameters are named with uppercase letters and underscores, for example
HTML_CACHE_ON
;
- Language variables are named with uppercase letters and underscores, for example
MY_LANG
The following underlined language variables are usually used for system language variables, such_CLASS_NOT_EXIST_
;
- There is no mandatory specification for variable naming, which can be implemented according to team specifications;
- The default thinkphp template file is
.html
Suffix (can be modified through configuration );
- Data Tables and fields are named with lowercase underscores (_). Note that the field name must not start with an underscore. For example
think_user
Table anduser_name
The field is written correctly, similar_username
Such data table fields may be filtered out.
Special case: In thinkphp, there is a special function naming case, that is, a single-letter upper-case function, which is usually a quick definition of some operations, or has a special role. For example, Methods A, D, S, and l have special meanings and will be understood later.
Because thinkphp uses UTF-8 encoding by default, make sure that your program files are saved in UTF-8 encoding format and remove the BOM header (there are many ways to remove the BOM header information, different editors have different setting methods, and you can also use tools for unified detection and processing). Otherwise, many unexpected problems may occur.
Development Suggestions
During thinkphp development, we provide the following suggestions to make your development easier:
- Follow the naming and directory specifications of the framework;
- Enable the debugging mode as much as possible during the development process to detect problems as soon as possible;
- Take a look at the log files to find hidden problems;
- Develop a good habit of getting input variables using the I function;
- When the update or environment changes, the first problem is to clear the runtime directory;