当前位置:网站首页>PHP code specification
PHP code specification
2022-06-23 03:05:00 【User 1349575】
Naming specification
Θ Class files are .class.php For the suffix , Use the hump method to name , And capitalize the first letter , for example Pay.class.php;
Θ Class name and directory _ Consistent file names . for example : Class name Zend_Autoloader The directory of is Zend/Autoloader.class.php;
Θ Functions are named using lowercase letters and underscores . for example :get_client_ip;
Θ The method is named by hump method , The first letter is lowercase or underlined "_", for example listComment(),_getResource(), Usually, methods that start with an underline are private methods ;
Θ Attributes are named using the hump method , The first letter is lowercase or underlined "_", Such as $username,$_instance, In general, properties that begin with an underline are private properties ;
Θ Constants are capitalized and underlined "_" name , Such as "HOME_URL";
Common nouns
1>list Noun ( singular ), Such as listApple, At first glance, we know to read the apple list , We don't have to write getApples perhaps listApples or readApples—— because get We specify that it is generally used to read single data , Such as getApple.listApples No addition s We also know it is to take the apple list ( Ensure that variable naming is as short as possible );
2>get Noun ( singular );
3> Noun Total, Indicates the total number of something . Such as expenseTotal;
4>found: Indicates whether a value has been found ;
5>uccess or ok: Whether an operation is successful ;
6>done: Whether a project has been completed ;
7>error: Whether there is a mistake ;
8>result: The result returned
Code refactoring
1. Try to control the code in a function or method in one screen .
2. Methods not used in the class are deleted randomly .
3. Modify other people's methods in class , To sign .
4. Write a in each module readme file ( Description or code description for complex business ).
5. Try to let each class do its own thing , Each function does one thing .
Common code
use && or || Simplified operation
Before simplification :
$a=1;$b = 0;if(isset($a)){ $b=1; print($b."\n");} if($b!=0){ print($b."\n");}simplified :
$a=1;$b = 0;isset($a) && ($b=1) && print($b."\n");$b == 0 || print($b."\n");
Obviously the code looks more tidy , It's simpler !
Judge "==" when , We may put "==" It's written in "=", In this way bug It is difficult for us to debug the diagram . therefore , Put the constants first , The compiler can tell .
Before :
$a = 1;if($a = 1){ echo '$a == 1';}after :
$a = 1;if(1 = $a){ echo '$a == 1';}obvious , If constants are put in front , The compiler can determine the error .
Normal format :
$a = 1;if(1 == $a){ echo '$a == 1';}Look up table method
Before :
/* Error code :4,5,7,8 Return to the status when 1, The error code is 1,3,6 Return to status 2*/$error = 4;$state = 0;if($error == 4 || $error == 5 || $error == 7 || $error == 8){ $state = 1;} if($error == 1 || $error == 3 || $error == 6){ $state = 2;}echo "$state \n";after :
/* Error code :4,5,7,8 Return to the status when 1, The error code is 1,3,6 Return to status 2*/$error = 4;$state = 0; $arr = array(4 => 1, 5 => 1, 7 => 1, 8 => 1, 1 => 2, 3 => 2, 6 => 2);isset($arr[$error]) && ($state = $arr[$error]); echo "$state \n";
Obviously, the code is more concise , More clearly , Easier to understand , Faster, too !
summary
I originally wanted to put some design patterns into common code , But too much , It's not easy to put . These are just small parts !
边栏推荐
- Line to line interviewer | one side of ByteDance
- An article shows you the difference between high fidelity and low fidelity prototypes
- New uniapp+uniui background management uniuadmin
- Great WPF open source control library newbeecoder UI
- Reading redis source code (II) underlying data structure
- Why can only a small number of condition type prices be maintained in me12 of SAP mm?
- Use micro build to realize search function
- How to use pictures in Excel in PPT template
- Integrate Tencent maps with micro build and low code
- How to make special labels for books
猜你喜欢

8. greed

6. template for integer and real number dichotomy

Soft exam information system project manager_ Contract Law_ Copyright_ Implementation Regulations - Senior Information System Project Manager of soft exam 030

How to store, manage and view family photos in an orderly manner?

Vulnhub DC-5

Soft exam information system project manager_ Information system comprehensive testing and management - Senior Information System Project Manager of soft test 027
What is sitelock? What is the function?

C language series - Section 4 - arrays

5. concept of ruler method
随机推荐
Record a penetration caused by log4j
Why do I use index, query or slow?
51. numerical arrangement
Build a weather forecast applet using a widget
Handlebars dynamic adjustment
How to set jewelry label paper
Reading redis source code (II) underlying data structure
No error is reported when using the Gorm framework to create a table, but the data cannot be inserted successfully
Section 6: basic configuration I of spingboot
Optimization method of live weak network
SAP mm initial transaction code MEK1 maintenance pb00 price
Qingdao stadium has made headlines again, but it has nothing to do with sports
[data preparation and Feature Engineering] perceived data
Chaoscraft: join your girlfriend in Hackathon show -- Interview with the skate team
Detailed explanation of online reputation management
Win11 client remote ECS black screen
Autowired usage
Reading redis source code (V) master-slave replication and sentinel mechanism
Markdown - enter a score (typora, latex)
Quickly grab the red envelope cover of Tencent blue whale New Year! Slow hands!