File files should be saved in Unicode (UTF-8) encoding. Do not use the byte sequence mark (BOM ). Unlike UTF-16 and UTF-32, UTF-8-encoded files do not need to specify the byte order, and the bytecode mark (BOM) produces unexpected output in PHP, the application is blocked from setting its own header information. The Unix-type
File files should be saved in Unicode (UTF-8) encoding. Do not use the byte sequence mark (BOM ). Unlike UTF-16 and UTF-32, UTF-8-encoded files do not need to specify the byte order, and the bytecode mark (BOM) produces unexpected output in PHP, the application is blocked from setting its own header information. The Unix-type
File Format
The file should be saved in Unicode (UTF-8) encoding. Do not use the byte sequence mark (BOM ). Unlike UTF-16 and UTF-32, UTF-8-encoded files do not need to specify the byte order, and the bytecode mark (BOM) produces unexpected output in PHP, the application is blocked from setting its own header information. The line terminator (LF) in Unix format should be used ).
Improper:
Echo "Here's my code! ";
?>
Appropriate:
Echo "Here's my code! ";
/* End of file myfile. php */
/* Location:./system/modules/mymodule/myfile. php */
Naming rules for classes and methods (functions)
The first letter of the class name should be capitalized. If the name is composed of multiple words, the words must be separated by underscores. Do not use the camel naming method. The names of all other methods in the class should be in full lowercase and the name can clearly specify the purpose of this function. It is best to start with a verb. Avoid long and redundant names as much as possible
Improper:
Class superclass
Class SuperClass
Appropriate:
Class Super_class
Examples of improper and appropriate method names:
Improper:
Function fileproperties () // The method name does not have a clear description or underline to separate words.
Function fileProperties () // The method name is not clearly described and is named using the hump method.
Function getfileproperties () // You can also! However, you forget to underline the word segmentation.
Function getFileProperties () // name using the hump method
Function get_the_file_properties_from_the_file () // The method name is too long.
Appropriate:
Function get_file_properties () // clear Method Name Description. Separate words with underscores (_). lowercase letters are all used.
Variable name
The naming rules of variables are very similar to those of methods. That is to say, the variable name should only contain lower-case letters, separated by underscores, and can properly specify the purpose and content of the variable. The short and meaningless variable names should be used only as iterators in the for () loop.
Improper:
$ J = 'foo'; // The Single-character variable should be used only as the for () Loop Variable
$ Str // uppercase letters are used
$ BufferedText // The bucket name is used, and the variable name should be shorter with clear syntax meanings.
$ Groupid // multiple phrases, which should be separated by underscores
$ Name_of_last_city_used // too long
Appropriate:
For ($ j = 0; $ j <10; $ j ++)
$ Str $ buffer $ group_id $ last_city
Note
Generally, the code should be annotated in detail. This not only helps programmers who lack experience to describe the code process and intent, but also provides you with a wealth of content that can be well understood when you look at your code in a few months. Annotations do not have a required format, but we recommend the following format.
DocBlock annotations must be written before the declaration of classes and methods so that they can be captured by the integrated development environment (IDE:
/**
* Super Class
* @ Package Package Name
* @ Subpackage Subpackage
* @ Category Category
* @ Author Author Name
* @ Link http://example.com
*/
When you use line comments, leave a blank line between the large comment block and the code.
Constant
Constants must be named in uppercase, and all other naming rules are the same as variables. Always use CodeIgniter constants, such as LASH, LD, RD, PATH_CACHE, and so on when appropriate.
Improper:
MyConstant // words are separated by no underline, and not all uppercase letters are used
N // a single letter cannot be used as a constant
S_C_VER // The constant name has no clear meaning
$ Str = str_replace ('{foo}', 'bar', $ str); // shocould use LD and RD constants
Appropriate:
MY_CONSTANT
NEWLINE SUPER_CLASS_VERSION
$ Str = str_replace (LD. 'foo'. RD, 'bar', $ str );
TRUE, FALSE, and NULL
TRUE, FALSE, and NULL keywords should always be fully capitalized.
Improper:
If ($ foo = true) $ bar = false;
Function foo ($ bar = null)
Appropriate:
If ($ foo = TRUE)
$ Bar = FALSE;
Function foo ($ bar = NULL)
Logical operators
| Sometimes difficult to identify because it is not clear enough on some output devices (may look like a number 11 ). & takes precedence over AND, but both can be accepted AND used! You must add a space before and after it.
Improper:
If ($ foo | $ bar)
If ($ foo AND $ bar) // yes, but is not highlighted by common syntax programs)
If (! $ Foo)
If (! Is_array ($ foo ))
Appropriate:
If ($ foo OR $ bar)
If ($ foo & $ bar) // We recommend if (! $ Foo) if (! Is_array ($ foo ))
Comparison return value and type ing
If some PHP functions fail to be executed, FALSE is returned, but there may also be a valid return value "" or 0, which will be calculated as FALSE in loose comparison. when using these return values in conditional statements, make an explicit comparison to ensure that the return value is the expected type rather than a value with a loose type.
Follow this strict method when returning and checking your own variables. Use = and when necessary! =.
Improper:
// If 'foo' is located at the start of the string, strpos returns 0,
// The result of condition determination is TRUE.
If (strpos ($ str, 'foo') = FALSE)
Appropriate:
If (strpos ($ str, 'foo') = FALSE)
Improper:
Function build_string ($ str = ""){
If ($ str = "") // uh-oh! What if the passed parameter is FALSE or an integer 0? {}
}
Appropriate:
Function build_string ($ str = ""){
If ($ str = ""){}
}
Space in the file (in CodeIgniter)
No space is allowed before and after PHP start marking. The output has been cached, so spaces in the file will cause CodeIgniter to start output before outputting its own content. This will cause CodeIgniter to fail to output the correct header. In the following example, you can select the text with the mouse to see the spaces that should not be.
Improper:
Appropriate:
Compatibility
Unless otherwise specified in your additional component documentation, all code must be compatible with PHP 5.1 and later versions. In addition, do not use functions that depend on libraries that are not installed by default, unless your code contains an alternative method when the function is unavailable, alternatively, you clearly stated in the document that some libraries are required for your additional components.
Use common words to name classes and files
When your class or file name is a common term or is likely to have the same name as another PHP script, use a unique prefix to avoid conflicts. You must always understand this: your end users may run additional components or PHP scripts from other third parties. Select a prefix that uniquely identifies a developer or company.
Improper:
Class Email pi. email. php
Class Xml ext. xml. php
Class Import mod. import. php
Appropriate:
Class Pre_email pi. pre_email.php
Class Pre_xml ext. pre_xml.php
Class Pre_import mod. pre_import.php
Database Table Name
Any table used by your additional components must use the 'exp _ 'prefix, and then a prefix that uniquely identifies the developer or company, the last is a brief descriptive table name. You do not need to worry about the database prefix used during installation, because the CodeIgniter database class will automatically convert 'exp _ 'according to the actual situation.
Improper:
Email_addresses // The two prefixes are missing
Pre_email_addresses // The exp _ prefix is missing.
Exp_email_addresses // the unique prefix is missing.
Appropriate:
Exp_pre_email_addresses
Note: The MySQL table name cannot exceed 64 characters. The table names that exceed this limit are unreasonable, so this should not be a problem. For example, the following table names have one more character than the maximum limit. This is silly, isn't it? Exp_pre_email_addresses_of_registered_users_in_seattle_washington
One file and one class
The classes used by your additional components should follow the principle of one file and one class, unless these classes are closely related. One example of CodeIgniter files containing multiple classes is database files, including DB and DB_Cache classes, and Magpie plug-ins, including Magpie and Snoopy classes.
Space
Use tab in the code to replace spaces. This may seem like a trivial matter, but replacing spaces with tabs helps developers who read your code customize indentation in their respective applications. Another advantage is that the files saved in this way are slightly more compact.
Line feed
The file must be saved using Unix line breaks. This is more important for developers in Windows, but in any case, make sure that your text editor has been set to use Unix line breaks to save files.
Code indent
Use Allman indent. Except for the class declaration, parentheses always occupy only one row, and the indentation is the same as the control statement that "belongs to" it.
Inappropriate:
Function foo ($ bar ){
//...
}
Foreach ($ arr as $ key => $ val ){
//...
}
If ($ foo = $ bar ){
//...
} Else {
//...
}
Appropriate:
Function foo ($ bar)
{
//...
}
Foreach ($ arr as $ key => $ val)
{
//...
}
If ($ foo = $ bar ){
//...
}
Else
{//...
}
Square brackets and space characters in parentheses
Generally, do not add any space characters in square brackets "[]" and parentheses. The only exception is to improve readability and differentiate them from functions. In the brackets used to control the PHP syntax structure that accepts parameters, a space character (declare, do-while, elseif, for, foreach, if, switch, while ).
Inappropriate:
$ Arr [$ foo] = 'foo ';
Correct:
$ Arr [$ foo] = 'foo'; // there is no space in the square brackets of the array key value
Inappropriate:
Function foo ($ bar ){}
Correct:
Function foo ($ bar) // there is no space in the parentheses of the function declaration {}
Inappropriate:
Foreach ($ query-> result () as $ row) // there is a space after the PHP syntax control structure, but not in parentheses
Correct: foreach ($ query-> result () as $ row)
Localized text
All texts output on the control panel should use language variables in the lang file to allow localization.
Incorrect:
Return "Invalid Selection ";
Correct:
Return $ this-> lang-> line ('invalid _ selection ');
Private methods and variables
The code is like some tools/helper class methods encapsulated to be called by other public functions in the class. They should be prefixed with an underscore.
Convert_text () // public Method
_ Convert_text () // Private Method
Short mark
Keep using the PHP complete tag to prevent the server from supporting the short tag, that is, the short_open_tag is not enabled. (This is not true because the new CI version solves the problem that the server does not support short tags. However, we recommend that you use full tags)
Incorrect:
Correct:
One statement per line
Do not write multiple statements in one row.
Incorrect:
$ Foo = 'false'; $ bar = 'that '; $ bat = str_replace ($ foo, $ bar, $ bag );
Correct:
$ Foo = 'foo ';
$ Bar = 'that ';
$ Bat = str_replace ($ foo, $ bar, $ bag );
String
Always use single quotes unless you need to parse the variable, use braces if you need to parse the variable.
If a string contains single quotes, you can use double quotation marks.
Incorrect:
"My String" // No parsing variable, no double quotation marks required
"My string $ foo" // brackets must be used to parse the variable
'Select foo FROM bar WHERE baz = \ 'bag \ ''// when you need to escape single quotation marks'', this is ugly to write. Double quotation marks can be used.
Correct:
'My string'
"My string {$ foo }"
"SELECT foo FROM bar WHERE baz = 'bag '"