PHP Coding Standard
Class:
1. Class name using the big hump nomenclature, such as Footest, the file name in lowercase. class.php, such as footest.class.php, the curly braces stand alone
2. Class methods with small humps, such as testfunction (), curly braces separate rows
3. Properties in class
4. Class attribute variables are lowercase, multiple words are separated by _
5. Exposed to the user, such as the Controller class method in lowercase, easy to unify the URL style
6. The constant in class is capitalized, and multiple words are separated by _
7. Class static variable lowercase, and add s_ prefix, more intuitive
8. Need to use abstract,final, must be written in front of the modifier, static written behind the modifier
9. Too many parameters, when divided into multiple columns, each row alone
Rest
1. Before and after operators need spaces such as +,-, *,.,/etc., comparison operator = =, >=, ternary operator?:
2.if/elif/else/foreach/for/while/do, and so on, the right curly brace alone, the left curly brace is not a separate row, the keyword and the left curly braces between the empty grid
3. Local variables in lowercase, multiple words with _ Division
4. Function name in lowercase, multiple words separated by _
All keywords such as 5.false,true,null must be lowercase
6. The parameters in the method each comma after the space, the rest of the space
7. Empty one square before opening parenthesis
8. Do not add meaningless variables, such as $a = Strtolower ($b); return $a; direct return strtolower ($b);
Example:
<?PHP/** * Created by Phpstorm. * User:ant * version:1.0 * DATE:2017/5/7 * time:16:32*/Define ("MALE",0);d Efine ("Famale",1);/** * Abstract class*/Abstract classfather{Abstract Publicfunction Run ();}/** * Test class*/classFooBar extends father{ConstAdult =1; Private$sex; protected$age; /** contruct * @param int $sex * @param int $age*/ Publicfunction __construct ($sex, $age) {$ This->sex =$sex; $ This->age =$age; } /** Run*/ Publicfunction Run () {echo $ This->age.'|'. $ This-sex; } /** Return Array*/ Publicfunction info () {returnArray ($ This->sex, $ This-Age ); } /** Set age * @param int $age*/ Publicfunction Setage ($age) {$ This->age =$age; } /** @param string $val*/ Public Staticfunction Factory ($val) {if('MySQL'===$val) { return NewMySql (); } Else { return Newmysqli (); } } /** @return Boolean*/Final Publicfunction Isadult () {return$ This->age > Self::adult?true:false; } Publicfunction Docalc () { for($i =0; $i <Ten; $i + +) { //........... } if($i <Ten) { //...........} elseif ($i < -) { //...........}Else { //...........} }} $age= -; $foo _bar=NewFooBar ($age, Famale); $foo _bar-run (); $foo _bar->setage ( $);
PHP Coding Specification