简体   繁体   English

特定于PHP-干净的代码,命名约定和文档

[英]PHP specific - clean code, naming conventions and documentation

What are some of the best practices for clean code, naming conventions and documentation for PHP? PHP的干净代码,命名约定和文档的最佳实践是什么?

I see users/people saying this is a bad practice, Example: 我看到用户/人说这是一种不好的做法,例如:

// Create an array to hold x values
$arr_x = array();

That this is a unnecessary comment cause the syntax alone explains the functionality. 这是不必要的注释,仅语法会解释功能。 That is should be more of a header comment that describes the script/function functionality rather than the variables/flow of the program. 那应该更多地是描述脚本/功能功能的标题注释,而不是程序的变量/流程。 Example

/**
 * Create an array
 */
function create_array() {
   return array();
}

$arr_x = create_array();

// This is just to show the comments and the code is not tested or used except for this example

This has lead me down the path of proper syntax, coding and documentation (The reason for the title naming). 这使我无法正确使用语法,编码和文档(标题命名的原因)。

what is acceptable for variable, functions and script naming conventions or is this personal preference? 变量,函数和脚本命名约定可以接受什么,或者这是个人喜好?

$varX
function varX()
varX.php

or 要么

$var_x
function var_x()
var_x.php

I'm trying to find if there is a standard I should be conforming to. 我正在尝试确定是否应该遵循一个标准。 Thanks 谢谢

Zend Framework has a PHP coding standard document here that should cover things like how to name variables and functions. Zend Framework 在这里有一个PHP编码标准文档其中应涵盖诸如如何命名变量和函数之类的内容。

When and how to comment is much less platform-specific. 何时以及如何发表评论的方式与平台无关。 I think a good general rule is to comment on why something is done, rather than how . 我认为一个好的一般规则是评论为什么要做某事,而不是如何做。 The code should be written clearly enough to make how obvious. 该代码应该写得明明足以使多么明显。 (There are exceptions, of course, such as the use of particularly complex algorithms that may need explanation.) (当然,有例外,例如使用可能需要解释的特别复杂的算法。)

There is no standard, just the opinions of developers. 没有标准,只有开发人员的意见。

I prefer to have variables delimited by underscores: 我更喜欢用下划线定界的变量:

$my_var

But for functions I prefer camel-case: 但是对于功能我更喜欢驼峰式:

function myFunction() {}

As for comments, yes sometimes comments such as // create array are not required at all, but don't be put off using one liners, they won't slow your script down when it is executed. 至于注释,是的,有时根本不需要// create array类的注释,但是不要用一个内衬将它们拖延,它们不会在脚本执行时减慢速度。 I like to use one lines to neatly describe each step of a complex script. 我喜欢用一行简洁地描述复杂脚本的每一步。

As long as your code is readable for you and your fellow developers (others on a project, 3rd part companies etc) then you will be fine. 只要您和您的其他开发人员(项目中的其他人,第三部分公司等)可读代码,那么您就可以了。

What's most important is to be consistent. 最重要的是要保持一致。 Beyond the basics, like descriptive variable, function, and method names, all that really matters is consistency. 除了描述性变量,函数和方法名之类的基础知识之外,真正重要的是一致性。

If you don't want to think too hard about it, feel free to use one of the popular coding style guides like the one from the PEAR project, or the Zend Framework standard that JacobM Just posted. 如果您不想太认真考虑,可以随意使用一种流行的编码样式指南,例如PEAR项目中的一种,或者JacobM刚刚发布的Zend Framework标准。

I use codeigniter, here is their style guide. 我使用codeigniter,这是他们的风格指南。
http://codeigniter.com/user_guide/general/styleguide.html http://codeigniter.com/user_guide/general/styleguide.html

找到php_codesniffer支持的您喜欢的标准或与现有代码库最匹配的标准 -并安装该工具-至少要有一个可用的工具来检查您的代码是否有差异。

One important thing is consistency. 重要的一件事是一致性。 Whatever you pick for your development team - any of the standards mentioned above - make sure that EVERYONE in your development organization is sticking to it. 无论您为开发团队选择什么(上面提到的任何标准),都要确保开发组织中的每个人都遵守它。 Otherwise, code will be very hard to read, and things will be hard to code-review. 否则,代码将很难阅读,并且很难进行代码审查。

Drupal is one of the biggest open source codebase written in PHP. Drupal是用PHP编写的最大的开源代码库之一。

They must have some good code convention. 他们必须具有一些良好的代码约定。

http://drupal.org/coding-standards http://drupal.org/coding-standards

Coding standards have been changing in php. 编码标准已在php中更改。 if you look at the older frameworks they all use Camel case, which in my opinion can lead to errors in the code. 如果您查看旧的框架,它们都使用Camel大小写,在我看来,这可能会导致代码错误。 it makes sense for a language like java, but not php. 对于像Java这样的语言,但对php来说却没有道理。

The more recent coding standards and frame works avoid using cammel case , and have a preference for lower_case underscore seperated variable names. 较新的编码标准和框架工作避免使用cammel case,并且优先使用lower_case下划线下划线分隔的变量名称。 eg: fat_yak, rather than fatYak. 例如:fat_yak,而不是fatYak。

The problem with php is that it will accept a new variable that has been undeclared, and since Case is important, it is possible to have two variables with the same name, but different case. php的问题在于它将接受一个尚未声明的新变量,并且由于Case很重要,因此可能有两个名称相同但大小写不同的变量。 Hence inmho it is important to always use lower case with variables, to avoid simple errors, which may go undetected otherwise. 因此,重要的是始终将小写字母与变量一起使用,以避免简单的错误,否则可能不会被发现。 The same principle should be extended to method names, as the same issue will be encountered when writing extended classes and overwriting method names. 应该将相同的原则扩展到方法名称,因为在编写扩展类和覆盖方法名称时会遇到相同的问题。 (it is possible to misplace a capital letter and end up with a second function, rather than replacing the original function as you intended.) (有可能放错大写字母并以第二个功能结尾,而不是按预期替换原始功能。)

I think there are some very fine coding standards out there that are spoiled by this camelCase aspect. 我认为,有一些非常好的编码标准会被camelCase方面所破坏。

this principle should also be extended to file names. 此原则也应扩展到文件名。 given unix servers differ from windows servers in regards to case, imho many of teh problems can be avoided by always using lowercase. 由于unix服务器与Windows服务器在大小写上有所不同,因此始终使用小写字母可以避免很多问题。 non the less naming classes with a capital starting letter is probably a nessescary evil. 并非如此,以大写字母开头的较不命名的阶级可能是卑鄙的邪恶。

Using CamelCase in class names is fine. 在类名中使用CamelCase很好。 If you make a mistake here it will be picked up straight away. 如果您在这里输入错误,它将立即被收取。 In fact using a capital letter at the start of a class is mandatory for your own sanity. 实际上,为了您自己的理智,在上课开始时必须使用大写字母。 (I would name them like this Fat_yak, not FatYak, but i am in the minority on that one, so may keep my mouth shut.. although it would make naming files easier.. eg: Fat_yak.php rather than FatYak.php) (我会以这样的名字命名为Fat_yak,而不是FatYak,但我在那方面是少数,所以虽然可以使文件命名更容易..例如:Fat_yak.php而不是FatYak.php,但还是可以闭嘴。

using 4 spaces instead of a tab is a very useful idea, especially if many different editors are used. 使用4个空格代替制表符是一个非常有用的想法,尤其是在使用许多不同的编辑器的情况下。 (the code will look the same on all editors) (代码在所有编辑器上看起来都一样)

everything else is a 50-50 proposition.. and each standard seems to choose one of two options.. This is the disappointing aspect of coding standards, in that a clear leader has not emerged. 其他所有事情都是50-50的命题..每个标准似乎都选择了两个选项之一。.这是编码标准令人失望的方面,因为还没有一个明确的领导者出现。

eg: 
"true" or "TRUE"

eg:
function blah(){

}

or

function blah()
{

}

This I'd call an antipattern. 我将其称为反模式。 What will you do when there will be some data type change? 当数据类型发生变化时,您将怎么办? Will you just change the whole project and maybe many other projects that use your code? 您会更改整个项目,还是可能更改使用代码的许多其他项目?

I'd rather use simple: 我宁愿使用简单的方法:

$x
function x()
x.php

JacobM just posted a good PHP standards doc. JacobM刚刚发布了一个很好的PHP标准文档。 However, if I'm editing or adding on to existing code, I try to follow the style laid out by the previous author. 但是,如果我要编辑或添加现有代码,则尝试遵循前一位作者提出的样式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM