2 コーディング基準
# 新機能を追加する
テストがない新機能は追加されません。リポジトリにコミットする前にテストが通っているべきです。
# インデント
インデントには1つのタブを使用します。
インデントは次のようになります:
<?php
// ベースレベル
// レベル 1
// レベル 2
// レベル 1
// ベースレベル
?> <?php// ベースレベル// レベル 1// レベル 2// レベル 1// ベースレベル?>
あるいは:
$booleanVariable = true;
$stringVariable = "moose";
if ($booleanVariable) {
echo "Boolean value is true";
if ($stringVariable == "moose") {
echo "We have encountered a moose";
}
}
$booleanVariable = true;$stringVariable = "moose";if ($booleanVariable) {echo "Boolean value is true";if ($stringVariable == "moose") {echo "We have encountered a moose";}}
# Control Structures
このセクションには保留されている変更があります. More information about translations
Control structures are for example "if", "for", "foreach", "while", "switch" etc. Below, an example with "if":
<?php
if ((expr_1) || (expr_2)) {
// action_1;
} elseif (!(expr_3) && (expr_4)) {
// action_2;
} else {
// default_action;
}
?>
<?phpif ((expr_1) || (expr_2)) {// action_1;} elseif (!(expr_3) && (expr_4)) {// action_2;} else {// default_action;}?>
- In the control structures there should be 1 (one) space before the first parenthesis and 1 (one) space between the last parenthesis and the opening bracket.
- Always use curly brackets in control structures, even if they are not needed. They increase the readability of the code, and they give you fewer logical errors.
- Opening curly brackets should be placed on the same line as the control structure. Closing curly brackets should be placed on new lines, and they should have same indentation level as the control structure. The statement included in curly brackets should begin on a new line, and code contained within it should gain a new level of indentation.
<?php
// wrong - no brackets, badly placed statement
if (expr) statement;
// wrong - no brackets
if (expr)
statement;
// good
if (expr) {
statement;
}
?>
<?php// wrong - no brackets, badly placed statementif (expr) statement;// wrong - no bracketsif (expr)statement;// goodif (expr) {statement;}?>
# Ternary Operator
このセクションには保留されている変更があります. More information about translations
Ternary operators are permissible when the entire ternary operation fits on one line. Longer ternaries should be split into if else statements. Ternary operators should not ever be nested. Optionally parentheses can be used around the condition check of the ternary for clarity.
//Good, simple and readable $variable = isset($options['variable']) ? $options['variable'] : true; //Nested ternaries are bad $variable = isset($options['variable']) ? isset($options['othervar']) ? true : false : false;
//Good, simple and readable$variable = isset($options['variable']) ? $options['variable'] : true;//Nested ternaries are bad$variable = isset($options['variable']) ? isset($options['othervar']) ? true : false : false;
# 関数のコール
The original text for this section has changed since it was translated. Please help resolve this difference. You can:
関数は、関数名と開始ブラケットの間に空白なしでコールされます。各関数コールの各パラメータの間には1つの空白があります。
<?php $var = foo($bar, $bar2, $bar3); ?>
<?php$var = foo($bar, $bar2, $bar3);?>
上記でみたように、イコールサイン (=) の両脇に1つの空白があります。コードの可読性を高めるために、イコールサインの前に空白(あるいはタブ)を追加することができますが、以下のような複数の関数コールの場合にのみ使用します:
<?php $varShort = foo($bar1); $variableLong = foo($bar1); ?>
<?php$varShort = foo($bar1);$variableLong = foo($bar1);?>
# メソッド定義
The original text for this section has changed since it was translated. Please help resolve this difference. You can:
関数定義の例:
<?php
function someFunction($arg1, $arg2 = '') {
if (expr) {
statement;
}
return $var;
}
?> <?phpfunction someFunction($arg1, $arg2 = '') {if (expr) {statement;}return $var;}?>
デフォルト値をもつパラメータは、関数定義の最後に置きます。関数が少なくとも true あるいは false のような何かを返すようにしてみましょう。そうすると関数コールが成功したかどうかを決定できます。
<?php
function connection(&$dsn, $persistent = false) {
if (is_array($dns)) {
$dns_info = &$dns;
} else {
$dns_info = BD::parseDNS(dns);
}
if (!($dns_info) || !($dns_info['phpType'])) {
return $this->addError();
}
return true;
}
?> <?phpfunction connection(&$dsn, $persistent = false) {if (is_array($dns)) {$dns_info = &$dns;} else {$dns_info = BD::parseDNS(dns);}if (!($dns_info) || !($dns_info['phpType'])) {return $this->addError();}return true;}?>
イコールサインの両脇には空白をいれます。
# コメントコード
The original text for this section has changed since it was translated. Please help resolve this difference. You can:
コメントはすべて英語で書くべきです。明確にコードのコメントブロックを記述します。
コメントには次のような phpDocumentor タグを記述できます:
- @access
- @author
- @copyright
- @deprecated
- @example
- @ignore
- @internal
- @link
- @see
- @since
- @tutorial
- @version
- inline {@internal}}
- inline {@inheritdoc}}
- inline {@link}}
PhpDoc タグは Java の JavaDoc タグに非常に似ています。DocBlock 行の最初のものである場合にのみタグが処理されます。たとえば:
<?php /** * Tag example. * @author this tag is parsed, but this @version is ignored * @version 1.0 this tag is also parsed */ ?>
<?php/*** Tag example.* @author this tag is parsed, but this @version is ignored* @version 1.0 this tag is also parsed*/?>
3つのインラインタグ({@internal}}, {@inheritdoc}},{@link}})があります。
<?php
/**
* Example of inline phpDoc tags.
*
* This function works hard with {@link foo()} to rule the world.
*/
function bar() {
}
function foo() {
}
?> <?php/*** Example of inline phpDoc tags.** This function works hard with {@link foo()} to rule the world.*/function bar() {}function foo() {}?>
# ファイルを include する
クラスやライブラリのファイルを include する際に、require_once 関数だけを常に使用します。
# PHP タグ
ショートタグ (<? ?>) ではなく、常に長いタグ (<?php ?>) を使用します。
# 命名規約
# 関数
The original text for this section has changed since it was translated. Please help resolve this difference. You can:
すべての関数はキャメルケースで記述します
<?php
function longFunctionName() {
}
?> <?phpfunction longFunctionName() {}?>
# クラス
クラス名はキャメルケースで記述します。たとえば:
<?php
class ExampleClass {
}
?> <?phpclass ExampleClass {}?>
# 変数
変数名はできるだけ内容を表すようにすべきですが、できるだけ短くすべきです。通常の変数は小文字で始め、複数の単語の場合はキャメルケースで記述すべきです。オブジェクトを含む変数は大文字で始め、その変数がそのオブジェクトのクラスに関連することを示すべきです。たとえば:
<?php
$user = 'John';
$users = array('John', 'Hans', 'Arne');
$Dispatcher = new Dispatcher();
?> <?php$user = 'John';$users = array('John', 'Hans', 'Arne');$Dispatcher = new Dispatcher();?>
# メンバーの視認性
メソッドや変数に PHP5 の private や protected キーワードを使用することはできないので、次のようなルールに従っています:
- protected メソッドや変数は、単一のアンダースコア ("_") で始めます。たとえば:
<?php class A { var _iAmAProtectedVariable; function _iAmAProtectedMethod() { /*...*/ } } ?><?phpclass A {var _iAmAProtectedVariable;function _iAmAProtectedMethod() {/*...*/}}?>
- private メソッドや変数は、2つのアンダースコア ("_") で始めます。たとえば:
<?php
class A {
var __iAmAPrivateVariable;
function __iAmAPrivateMethod() {
/*...*/
}
}
?>
<?phpclass A {var __iAmAPrivateVariable;function __iAmAPrivateMethod() {/*...*/}}?>
# アドレスの例Example addresses
The original text for this section has changed since it was translated. Please help resolve this difference. You can:
URL やメールアドレスの例にはすべて、"example.com", "example.org", "example.net" を使用します。たとえば:
- Email: someone@example.com
- WWW: http://www.example.com
- FTP: ftp://ftp.example.com
# ファイル
ファイル名は小文字で生成すべきです。ファイル名が複数の単語からなる場合は、アンダースコアで区切ります。たとえば:
long_file_name.php
# 変数の型
DocBlock で使用する変数の型:
| 型 | 内容 |
|---|---|
| mixed | 未定義 (あるいは複数) の型の変数 |
| integer | 整数型の変数 (すべての数値) |
| float | 実数型 (ポイント付きの数値) |
| boolean | 論理型 (true or false) |
| string | 文字列型 ("" あるいは ' ' の値). |
| array | 配列型 |
| object | オブジェクト |
| resource | リソース (たとえば mysql_connect() で返される値). |
mixed と型を指定した場合、不明かどうかあるいは何が有効であるかを示すべきであることを覚えておいてください。
# 定数
定数は大文字で定義すべきです:
<?php
define('CONSTANT', 1);
?>
<?phpdefine('CONSTANT', 1);?>
定数名が複数の単語からなる場合、アンダースコアで区切ります。たとえば:
<?php
define('LONG_NAMED_CONSTANT', 2);
?>
<?phpdefine('LONG_NAMED_CONSTANT', 2);?>

