构建知识体系之一--PHP代码规范
首秀啊!-----PHP代码规范
之前的博客“构建知识体系“中,提到了很多细节性的问题,接下来就以上个博客为纲,细细的讨论一下:
每个人,每个公司的规范可能不同,我只说一下自己的规范哈!
提高代码的可读性:
- 给代码加注释,读一份没有注释的代码,就像是唐僧西天取经,不经历九九八十一难,你就是看不懂!
- 给代码加空行,善于利用空行的代码就像是有节奏感的音乐,多一行太多,少一行太少!
- 30秒法则:其他的程序员完全理解你的成员函数代码,不超过30秒,否则,这份代码就比较难以维护
- 一行代码不要超过120个字符,否则换行缩进,一个函数最好在一个屏幕的大小内显示,最长不超过三个屏幕
- 一行代码只做一件事,同理一个函数也只负责一个功能。
- 循环比较大的时候不要使用$i这样的,起个有意义的名字
- while,if,else嵌套不要超过三层,要不30秒怎么读的懂?
- 一个函数最少的注释应该有作用、所有参数、所有返回值,最好注释上作者(追究责任
)
- 所有的缩进均为4个空格而非tab,所有的换行符均为unix换行符,为保证100%正确可以开启IDE的显示空白和不可见字符的功能
提升代码效率和健壮:
- 如未使用DB类的公共处理,则SQL语句查询前,应使用mysql_escape_string对查询串进行转义
- 在第一个引入的文件中定义好包含绝对路径的常量,其他引入的文件路径都常量开始
- 所有的类常量、静态变量、成员变量都应该提前申明,在申明的地方注释说明其用途,并将其值初始化为一个安全值
- 如未使用DB类的公共处理,则SQL语句查询前,应使用mysql_escape_string对查询串进行转义
- 调用外部接口和函数,要放在try/catch块中,也要检查返回值, 做足够健壮的错误处理,记录日志
- PHP文件尾部不闭合避免PHP标签避免输出多余的空行!!!
命名规则:
- 命名要有意义,有注释,其他程序员读的时候,一眼就能看懂,读懂,记住
- 所有不区分大小写的PHP关键字均使用小写,例如:true false null break等等
- 所有的变量、类成员变量、函数名、类成员函数名均采用驼峰式命名规范
- 所有的常量、类常量、静态变量等全部使用大写字母,下划线分割单词
- private和protected属性的类静态变量、类成员变量和类成员函数名在原有命名规范的基础上以下划线开头
- 文件名由字母、数字和下划线组成,首字符必须为字母,如果文件是一个类,则文件名应该为"类class.php",如"DBConfig.class.php",否则文件名全部为小写,多个单词下划线分隔,如"post_list.php"
- 类名称采用帕斯卡命名规则,即所有单词首字母大写,括号跟在类名后边间隔一个空格
- 类常量全部大写,用下划线分隔单词
争论性
- 所有的"{"左大括号都以空格紧跟前文,不能独立成为一行
- 对于关联数组,我们鼓励将代码分成多行,缩进每一行的KEY以对齐,"=>"左右
有很多没有写的,希望多多建议啊,让我们代码越来越规范
---本规范参照福州车友网络科技有限公司代码规范

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

WebStorm Mac version
Useful JavaScript development tools