2. 1. /**
2. * Short description for file
3. *
4. *
5. * @package Codegniter
6. * @created tai.nguyen
7. * @version 1.0
8. * @date 2013.03.04
9. */
10. class Student{
11. ....
12. }
1) File Format
- It' should be “UTF-8, no BOM” (recommended)
- Every file that contains PHP code must have a docblock
at the top of the file.
- Every function must have a docblock at the top
1. /**
2. * @Description Short description for the function
3. * @Created tai.nguyen
4. * @date 2013.03.04
5. */
6. function login(){
7. ....
8. }
3. - Class names: may only contain alphanumeric characters, the first letter of
each new word must be capitalized.
- Methods:
. Function names may only contain alphanumeric characters.
. Underscores are not permitted.
. Function names must always start with a lowercase letter.
. When a function name consists of more than one word, the first letter of each
new word must be capitalized.
. Each parameter is separated by ',' and one space.
1. function filterInput($usrId, $usrname){
2. ...
3. }
4.
5. function getElementById($inputId){
6. ...
7. }
8.
9. function widgetFactory(){
10. ...
11. }
4. . Avoid passing huge numbers of parameters to functions. Difficult to
remember the order of parameters. Consider passing an associative array
of named parameters instead.
9. function __construct( $address = '', $user = 0, $by = 0, $reason = '',
10. $timestamp = 0, $auto = 0, $expiry = '', $anonOnly = 0, $createAccount = 0, $enableAutoblock = 0,
11. $hideName = 0, $blockEmail = 0, $allowUsertalk = 0 )
12. {
13. ...
14. }
9. function __construct($params=array() ){
12. echo 'Your address: '. $params['address'];
13. $this->user = $params['user'];
14. ...
15. }
⇊
- Variable:
. Function names may only contain alphanumeric characters.
. Underscores are not permitted.
. Function names must always start with a lowercase letter.
. When a function name consists of more than one word, the first letter of each
new word must be capitalized.
3. $userId = getIdByEmail($email);
4. $userRecommand = getUserRecommand();
5. . Put spaces on either side of binary operators.
1. // No:
2. $a=$b+$c;
3.
4. // Yes:
5. $a = $b + $c;
. Global variable: start with “g” word
1. global $gSiteName;
. Global variable in a Class: start with underscore “_” characters
1. class Student{
2. var $_id;
3. var $_name;
4. public function __construct() {
5. }
6. }
6. - When a string is literal (contains no variable substitutions), "single quote"
should always be used to demarcate the string.
- Strings must be concatenated using the "." operator.
String Concatenation
1. $title = 'Vitalify' . ' ' . 'Asia';
- When concatenating strings with the "." operator. If line is so long, You should
break the statement into multiple lines to improve readability.
1. $sqlStr = 'SELECT `id`, `title`, `description` FROM `tbl_news` '
2. .'WHERE `id` NOT IN (1, 2, 3) AND `status`=1 '
3. .'ORDER BY `title` DESC LIMIT 0, 10';
7. - Short Open Tags. Always use full PHP opening tags,
in case a server does not have short_open_tag enabled.
- Don't start with empty line
1.
2. <?php
3. echo $foo;
4. ?>
1. <?php
2. echo $foo;
3. ?>
- Don't enter empty lines at the end of file.
1. <?php
2 class Student{
3. }
4.
5.
6. ?>
1. <?php
2 class Student{
3. }
4. ?>
<?php echo $foo; ?><? echo $foo; ?>
⇉
⇉
⇉
8. Others
- Remove all trailing whitespace at the end of your lines(Eclipse PDT provides)
9. - Set Tab is 4 whitespaces. Preferences > General > Editors > Text Editors
. “Displayed tab width” input “4”
. “Insert spaces for tabs” must be selected.