Programmer's Reference Guide

PHP ファイルの書式

命名規約

クラス

Zend Framework では、クラスの名前が保存先ディレクトリに直接対応するような 命名規約を採用しています。Zend Framework 標準ライブラリの最上位レベルのディレクトリは "Zend/" ディレクトリです。一方、Zend Framework 追加ライブラリの最上位レベルのディレクトリは "ZendX/" ディレクトリです。この配下に、すべてのクラスが階層構造で保存されます。

クラス名には英数字のみが使用できます。クラス名に数字を使用することは可能ですが、 ほとんどの場合はお勧めしません。アンダースコアはパス区切り文字としてのみ使用可能です。 ファイル名が "Zend/Db/Table.php" の場合、クラス名を "Zend_Db_Table" としなければなりません。

クラス名が複数の単語から成り立つ場合は、 それぞれの単語の最初の文字を大文字にしなければなりません。 大文字を連続して使用することはできません。例えば "Zend_PDF" というクラス名は許可されません。代わりに "Zend_Pdf" を使用します。

これらの規約によって、Zend Framework 上で擬似名前空間を定義しています。 PHP に名前空間機能が追加されるようになったら、Zend Framework もそれに対応させます。 それにより、開発者は自分のアプリケーションで名前空間を使用できるようになります。

標準ライブラリや追加ライブラリのクラス名を見れば、クラス名の命名規約のよい例となるでしょう。

注意: 重要: Zend Framework ライブラリとともに配布するが、 標準ライブラリや拡張ライブラリではないもの (たとえば、アプリケーションのコードや Zend 以外が作成したライブラリなど) については、"Zend_" や "ZendX_" で始まる名前は使用できません。

抽象クラス

一般に、抽象クラスの規約は通常の クラス と同じものとなります。追加の規則として、抽象クラスの名前は最後が "Abstract" (そしてその前にはアンダースコアはつけない) でなければなりません。 たとえば Zend_Controller_Plugin_Abstract は規約にそった名前ではありません。規約に従った名前は Zend_Controller_PluginAbstract あるいは Zend_Controller_Plugin_PluginAbstract となります。

注意: この命名規約が適用されるのは、Zend Framework 1.9.0 以降です。 それより前のバージョンではこの規約に従っていないものもあるかもしれませんが、 将来のバージョンでは規約に従うよう名前が変わる予定です。
The rationale for the change is due to namespace usage. As we look towards Zend Framework 2.0 and usage of PHP 5.3, we will be using namespaces. The easiest way to automate conversion to namespaces is to simply convert underscores to the namespace separator -- but under the old naming conventions, this leaves the classname as simply "Abstract" or "Interface" -- both of which are reserved keywords in PHP. If we prepend the (sub)component name to the classname, we can avoid these issues.
To illustrate the situation, consider converting the class Zend_Controller_Request_Abstract to use namespaces:

  1. namespace Zend\Controller\Request;
  2.  
  3. abstract class Abstract
  4. {
  5.     // ...
  6. }
Clearly, this will not work. Under the new naming conventions, however, this would become:
  1. namespace Zend\Controller\Request;
  2.  
  3. abstract class RequestAbstract
  4. {
  5.     // ...
  6. }
We still retain the semantics and namespace separation, while omitting the keyword issues; simultaneously, it better describes the abstract class.

インターフェイス

一般に、インターフェイスの規約は通常の クラス と同じものとなります。追加の規則として、インターフェイスの名前の最後は "Interface" (そしてその前にはアンダースコアはつけない) にすることもできます。 たとえば Zend_Controller_Plugin_Interface は規約にそった名前ではありません。規約に従った名前は Zend_Controller_PluginInterface あるいは Zend_Controller_Plugin_PluginInterface となります。

この規約は必須ではありませんが、強く推奨します。 そのファイルがクラスではなくインターフェイスを含むものであることが 開発者にわかりやすくなるからです。

注意: この命名規約が適用されるのは、Zend Framework 1.9.0 以降です。 それより前のバージョンではこの規約に従っていないものもあるかもしれませんが、 将来のバージョンでは規約に従うよう名前が変わる予定です。 この変更に関連する詳細については前節 をご覧ください。

ファイル名

すべてのファイルにおいて、使用可能な文字は英数字・アンダースコア およびダッシュ文字 ("-") のみです。空白文字は使用できません。

PHP コードを含むすべてのファイルの拡張子は ".php" でなければなりません。 ただしビュースクリプトは例外です。次の例は、Zend Framework のクラスに使用できるファイル名を示すものです。

  1. Zend/Db.php
  2.  
  3. Zend/Controller/Front.php
  4.  
  5. Zend/View/Helper/FormRadio.php

ファイル名は、上で説明したとおりの方式でクラス名と対応していなければなりません。

関数およびメソッド

関数名に含めることができるのは英数字のみです。 アンダースコアを使用してはいけません。 数字を含めることは可能ですが、ほとんどの場合はお勧めしません。

関数名は小文字で始めなければなりません。 関数名が複数の単語で構成されている場合は、 各単語の最初の文字を大文字にしなければなりません。 一般に、このフォーマットは "camelCaps" と呼ばれています。

関数名は省略しすぎないようにしましょう。 コードを理解しやすくするため、 現実的な範囲でできるだけ詳細な名前をつけるようにしましょう。

条件を満たす関数名の例を示します。

  1. filterInput()
  2.  
  3. getElementById()
  4.  
  5. widgetFactory()

オブジェクト指向のプログラミングでは、 インスタンス変数や静的変数にアクセスするためのメソッドは "get" あるいは "set" のいずれかで始めなければなりません。singleton や factory などのデザインパターンを使用する場合は、 メソッド名にパターンの名前を含めるようにしましょう。こうすることで、 どのパターンを使っているのかがわかりやすくなります。

オブジェクト内で "private" あるいは "protected" と宣言されているメソッドについては、メソッド名の最初にアンダースコア 1 文字をつけなければなりません。アンダースコアを使用できるのは、 この場合のみです。"public" と宣言されているメソッドについては、 決してアンダースコアで始めてはいけません。

グローバル関数は、できる限り使用しないようにしましょう。 このような関数は、静的クラスにまとめることを推奨します。

変数

変数名に含めることができるのは英数字のみです。 アンダースコアを使用してはいけません。 数字を含めることは可能ですが、ほとんどの場合はお勧めしません。

クラス内で "private" あるいは "protected" と宣言されている変数については、変数名の最初にアンダースコア 1 文字をつけなければなりません。アンダースコアを使用できるのは、 この場合のみです。"public" と宣言されている変数については、 決してアンダースコアで始めてはいけません。

関数名と同様 (上の 3.3 を参照ください)、 変数名も常に小文字で開始する "camelCaps" 方式を使用しなければなりません。

変数名は省略しすぎないようにしましょう。現実的な範囲で、 できるだけ詳細な名前をつけるべきです。"$i" や "$n" のような省略形が許されるのは、小さなループ内で使用する場合のみです。 ループが 20 行以上のコードになるようなら、 そのループ変数にはそれなりの名前をつけるべきです。

定数

定数名には英数字およびアンダースコアを使用できます。 定数名には数字を使用してもかまいません。

定数名は、常にすべて大文字にします。

定数名の単語の間はアンダースコアで区切らなければなりません。 例えば EMBED_SUPPRESS_EMBED_EXCEPTION は許されますが、 EMBED_SUPPRESSEMBEDEXCEPTION は許されません。

定数を宣言する際には、クラスのメンバとして "const" で定義しなければなりません。"define" によるグローバル定数の宣言も可能ですが、お勧めしません。


PHP ファイルの書式

Comments

This is a quote from this page:

"In general, abstract classes follow the same conventions as classes, with one additional rule: abstract class names must end in the term, "Abstract", and that term must not be preceded by an underscore. As an example, Zend_Controller_Plugin_Abstract is considered an invalid name, but Zend_Controller_PluginAbstract or Zend_Controller_Plugin_PluginAbstract would be valid names."

In the new Package Zend_cloud and Zend_Http from version 1.11.5 name of abstract classes are like this:

Zend_Http_UserAgent_AbstractDevice

Should it be Zend_Http_UserAgent_DeviceAbstract instead ?

Or am i wrong ?

Thanks for your answer.
thanks the article is very helpful..am new to zend framework ,but sure to master it...
Object methods public doesn't start by '_'. But Class method yes ?

public static function _unsetInstance()
{
self::$_registry = null;
}
@Nicolas
In ZF2 has been almost everything has been converted to Abstract as first term.

The case Zend_Controller_Plugin_AbstractPlugin is usefull if you plan to have a folder for your concrete implementations, for example Plugins.

The case Zend_Controller_AbstractPlugin seems only usefull in places where you implement the concrete classes otherwise. Wich for Plugins, it is not.

I couldn't find ZF2 conventions yet, but i'll continue searching the tracker and report that to be fixed here.

+ Add A Comment

Please do not report issues via comments; use the ZF Issue Tracker.

If you have a JIRA/Crowd account, we suggest you login first before commenting.

  • BBCode is allowed in the comment markup

  • Select a Version

    Languages Available

    Components

    Search the Manual