编辑“WordPress:WordPress Coding Standards

跳转至: 导航、​ 搜索
警告:您没有登录。如果您做出任意编辑,您的IP地址将会公开可见。如果您登录创建一个账户,您的编辑将归属于您的用户名,且将享受其他好处。

该编辑可以被撤销。 请检查下面的对比以核实您想要撤销的内容,然后发布下面的更改以完成撤销。

最后版本 您的文本
第1行: 第1行:
<span style="border:1px solid #000; text-align:center; float:right; padding:6px;"><strong>导航:</strong> [[WordPress:WordPress文档|上一级]] | [[WordPress]] | {{Template:WordPress导航}}</span>
<div style="clear:both;"></div>
一些WordPress代码结构遗留下来的部分对于PHP的开发在类型上是不一致的。 WordPress致力于通过帮助用户维护代码一致性,维持整洁易读的程度来逐渐改变这点。
一些WordPress代码结构遗留下来的部分对于PHP的开发在类型上是不一致的。 WordPress致力于通过帮助用户维护代码一致性,维持整洁易读的程度来逐渐改变这点。


第56行: 第54行:
</nowiki></pre>
</nowiki></pre>
;include_once  vs. require_once :了解 [http://us3.php.net/manual/en/function.include-once.php <tt>include_once</tt>] 和 [http://us3.php.net/manual/en/function.require-once.php <tt>require_once</tt>]之间的不同,适当的使用它们。引用[http://us3.php.net/manual/en/function.include.php关于include()的php手册页面]: "这两种结构除了在如何处理故障方面,其他的都是一样的。当require() 导致一个致命错误的时候,include() 生成一个警告。" 致命错误停止代码执行。
;include_once  vs. require_once :了解 [http://us3.php.net/manual/en/function.include-once.php <tt>include_once</tt>] 和 [http://us3.php.net/manual/en/function.require-once.php <tt>require_once</tt>]之间的不同,适当的使用它们。引用[http://us3.php.net/manual/en/function.include.php关于include()的php手册页面]: "这两种结构除了在如何处理故障方面,其他的都是一样的。当require() 导致一个致命错误的时候,include() 生成一个警告。" 致命错误停止代码执行。
;Regular expressions: Perl compatible regular expressions (PCRE, <tt>preg_</tt> functions) should be used in preference to their POSIX counterparts.
;No Shorthand PHP :'''Never use shorthand PHP start tags''' (<tt>&lt;? ... ?&gt;</tt> or <tt>&lt;?=$var?&gt;</tt>). Always use full PHP tags (<tt>&lt;?php ... ?&gt;</tt>). '''Make sure to remove trailing whitespace after closing PHP tag.'''


;常规表达: Perl 兼容常规表达(PCRE, <tt>preg_</tt> 函数)应该被优先用在 它们的POSIX 副本.  
;常规表达: Perl 兼容常规表达(PCRE, <tt>preg_</tt> 函数)应该被优先用在 它们的POSIX 副本.  


;不要使用简写PHP :'''不要使用简写PHP 开始标签''' (<tt>&lt;? ... ?&gt;</tt> 或者 <tt>&lt;?=$var?&gt;</tt>)。要一直使用完全的PHP 标签 (<tt>&lt;?php ... ?&gt;</tt>). '''确认关闭PHP标签后移除空白空格。'''
;不要使用简写PHP :'''不要使用简写PHP 开始标签''' (<tt>&lt;? ... ?&gt;</tt> 或者 <tt>&lt;?=$var?&gt;</tt>)。要一直使用完全的PHP 标签 (<tt>&lt;?php ... ?&gt;</tt>). '''确认关闭PHP标签后移除空白空格。'''
;Space Usage :Always put spaces after commas and on both sides of logical and assignment operators like <tt>"x == 23", "foo && bar", "array( 1, 2, 3 )",</tt> , as well as on both sides of the opening and closing parenthesis of <tt>if</tt>, <tt>elseif</tt>, <tt>foreach</tt>, <tt>for</tt> and <tt>switch</tt> blocks (e.g. <tt>foreach ( $foo as $bar ) { ...</tt>).  When defining a function, do it like so: <tt>function myfunction( $param1 = 'foo', $param2 = 'bar' ) {</tt> and when calling a function, do it like so: <tt>myfunction( $param1, funcparam( $param2 ) );</tt>


;空格用法 :在逗号后面要加一个空格,在逻辑和分配算子两边也要加上空格如 <tt>"x == 23", "foo && bar", "array( 1, 2, 3 )",</tt> , 同样在打开和关闭<tt>if</tt>, <tt>elseif</tt>, <tt>foreach</tt>, <tt>for</tt> 和 <tt>switch</tt> 语句的括号两边也加上空格 (如<tt>foreach ( $foo as $bar ) { ...</tt>).  定义一个函数的时候,这样做: <tt>function myfunction( $param1 = 'foo', $param2 = 'bar' ) {</tt> 在调用函数的时候,这样做: <tt>myfunction( $param1, funcparam( $param2 ) );</tt>
;空格用法 :在逗号后面要加一个空格,在逻辑和分配算子两边也要加上空格如 <tt>"x == 23", "foo && bar", "array( 1, 2, 3 )",</tt> , 同样在打开和关闭<tt>if</tt>, <tt>elseif</tt>, <tt>foreach</tt>, <tt>for</tt> 和 <tt>switch</tt> 语句的括号两边也加上空格 (如<tt>foreach ( $foo as $bar ) { ...</tt>).  定义一个函数的时候,这样做: <tt>function myfunction( $param1 = 'foo', $param2 = 'bar' ) {</tt> 在调用函数的时候,这样做: <tt>myfunction( $param1, funcparam( $param2 ) );</tt>
;Formatting SQL statements :When formatting SQL statements you may break it into several lines and indent if it is sufficiently complex to warrant it. Most statements work well as one line though. Always capitalize the SQL parts of the statement like <tt>UPDATE</tt> or <tt>WHERE</tt>.


;格式化 SQL 声明 :如果十分复杂而不能辨别的话,当格式化SQL声明的时候你可以把它分成几行,然后缩进。多数声明在一行内运行良好。声明的SQL部分如<tt>UPDATE</tt> 或者 <tt>WHERE</tt>一定要大写。
;格式化 SQL 声明 :如果十分复杂而不能辨别的话,当格式化SQL声明的时候你可以把它分成几行,然后缩进。多数声明在一行内运行良好。声明的SQL部分如<tt>UPDATE</tt> 或者 <tt>WHERE</tt>一定要大写。
:Functions that update the database should expect their parameters to lack SQL slash escaping when passed.  Escaping should be done as close to the time of the query as possible, preferably by using <tt>$wpdb-&gt;prepare()</tt>


:升级数据库的函数尽量减少参数应该盼望参数在传递过程中减少丢失。溢出应该在尽可能接近请求的时候完成。最适合的时候是正在使用 <tt>$wpdb-&gt;prepare()</tt>时。
:升级数据库的函数尽量减少参数应该盼望参数在传递过程中减少丢失。溢出应该在尽可能接近请求的时候完成。最适合的时候是正在使用 <tt>$wpdb-&gt;prepare()</tt>时。
:<tt>$wpdb-&gt;prepare()</tt> is a method that handles escaping, quoting, and int-casting for SQL queries.  It uses a subset of the <tt>sprintf()</tt> style of formatting.  Example :
<pre><nowiki>
$var = "dangerous'"; // raw data that may or may not need to be escaped
$id  = some_foo_number(); // data we expect to be an integer, but we're not certain
$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET post_title = %s WHERE ID = %d", $var, $id ) );
</nowiki></pre>
:<tt>$wpdb-&gt;prepare()</tt>是一个方法,为SQL请求控制溢出,引用和int-casting。它使用一个格式化的<tt>sprintf()</tt>格式的的子集。如 :
:<tt>$wpdb-&gt;prepare()</tt>是一个方法,为SQL请求控制溢出,引用和int-casting。它使用一个格式化的<tt>sprintf()</tt>格式的的子集。如 :


第74行: 第92行:
$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET post_title = %s WHERE ID = %d", $var, $id ) );
$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET post_title = %s WHERE ID = %d", $var, $id ) );
</nowiki></pre>
</nowiki></pre>
: <tt>%s</tt> is used for string placeholders and %d is used for integer placeholders.  Note that they are not 'quoted'!  <tt>$wpdb-&gt;prepare()</tt> will take care of escaping and quoting for us.  The benefit of this is that we don't have to remember to manually use <tt>$wpdb-&gt;escape()</tt>, and also that it is easy to see at a glance whether something has been escaped or not, because it happens right when the query happens.
: <tt>%s</tt> 是用于字符串占位符的,%d是用于整型占位符。注意它们没有被'引用'!  <tt>$wpdb-&gt;prepare()</tt>会照应好溢出和引用的。这样的好处就是我们不需要记住手动使用<tt>$wpdb-&gt;escape()</tt>,这样一眼就能看出是否有什么溢出,因为它在请求发生的那一刻就起作用了。
: <tt>%s</tt> 是用于字符串占位符的,%d是用于整型占位符。注意它们没有被'引用'!  <tt>$wpdb-&gt;prepare()</tt>会照应好溢出和引用的。这样的好处就是我们不需要记住手动使用<tt>$wpdb-&gt;escape()</tt>,这样一眼就能看出是否有什么溢出,因为它在请求发生的那一刻就起作用了。
;Database Queries :Avoid touching the database directly. If there is a defined function that can get the data you need, use it. Database abstraction (using functions instead of queries) helps keep your code forward-compatible and, in cases where results are cached in memory, it can be many times faster.  If you must touch the database, get in touch with some developers by posting a message to the [[WordPress:Mailing_Lists#Hackers|wp-hackers mailing list]].  They may want to consider creating a function for the next WordPress version to cover the functionality you wanted.
;数据库请求 :避免直接改动数据库。如果有一个定义过的函数可以得到你想要的数据,就用它,数据库提取(使用函数代替请求)对保证你的代码的向前兼容性有帮助,这样结果在内存中找到后,它可以用数倍更快的速度。如果你必须改动数据库的话,通过发布信息到[[WordPress:Mailing_Lists#Hackers|wp-hackers 邮件列表]]联系几个开发人员。他们可能会考虑为下一个WordPress 版本开发一个新的函数来达到你想要的功能。
;数据库请求 :避免直接改动数据库。如果有一个定义过的函数可以得到你想要的数据,就用它,数据库提取(使用函数代替请求)对保证你的代码的向前兼容性有帮助,这样结果在内存中找到后,它可以用数倍更快的速度。如果你必须改动数据库的话,通过发布信息到[[WordPress:Mailing_Lists#Hackers|wp-hackers 邮件列表]]联系几个开发人员。他们可能会考虑为下一个WordPress 版本开发一个新的函数来达到你想要的功能。
;Variables, functions, and operators :If you don't use a variable more than once, don't create it. This includes queries. Always use the [[WordPress:Function_Reference/wpdb Class|wpdb Class]] of functions when interacting with the database.


;变量, 函数和算子:如果你不使用变量,就不要创建。这包括请求在内。于数据库交换数据时使用函数[[WordPress:Function_Reference/wpdb Class|wpdb类]]。
;变量, 函数和算子:如果你不使用变量,就不要创建。这包括请求在内。于数据库交换数据时使用函数[[WordPress:Function_Reference/wpdb Class|wpdb类]]。
:[[WordPress:Wikipedia:Ternary_operation|Ternary]] operators are fine, but always have them test if the statement is true, not false. Otherwise it just gets confusing.
<pre><nowiki>// GOOD example:
// (if statement is true) ? (do this) : (if false, do this);
$musictype = ('jazz' == $music) ? 'cool' : 'blah';
</nowiki>
</pre>


:[[WordPress:Wikipedia:Ternary_operation|三重]]算子是可以的,声明为真时要记得测试,否则它会混淆。
:[[WordPress:Wikipedia:Ternary_operation|三重]]算子是可以的,声明为真时要记得测试,否则它会混淆。
第86行: 第120行:
</nowiki>
</nowiki>
</pre>
</pre>
:Another important point in the above example, when doing logical comparisons always put the variable on the right side, like above. If you forget an equal sign it'll throw a parse error instead of just evaluating true and executing the statement. It really takes no extra time to do, so if this saves one bug it's worth it.


:另外一个重点是在上边的例子中,当做逻辑比较的时候,要把变量放到右边,象上边那样。如果你忘记了等号,它会出现一个解析错误而不是判断为真或者停止声明。它并不占用过多的时间,所以这如果避免了一个错误,是值得的。
:另外一个重点是在上边的例子中,当做逻辑比较的时候,要把变量放到右边,象上边那样。如果你忘记了等号,它会出现一个解析错误而不是判断为真或者停止声明。它并不占用过多的时间,所以这如果避免了一个错误,是值得的。
:single line blocks can omit braces for brevity:


:单独行为了简洁省略括号
<pre><nowiki>
<pre><nowiki>
if ( condition )
if ( condition )
请注意,您对站长百科的所有贡献都可能被其他贡献者编辑,修改或删除。如果您不希望您的文字被任意修改和再散布,请不要提交。
您同时也要向我们保证您所提交的内容是您自己所作,或得自一个不受版权保护或相似自由的来源(参阅Wordpress-mediawiki:版权的细节)。 未经许可,请勿提交受版权保护的作品!
取消 编辑帮助(在新窗口中打开)

本页使用的模板: