LoginSignup
0
0

More than 3 years have passed since last update.

PSR-4: Autoloader 読了

Posted at

PSRとは

  • PSR(PHP Standards Recommendations)とは、PHP-FIG(PHP Framework Interop Group)が策定しているPHPコーディング規約を指す

PSR-4: Autoloader

PSR-4: Autoloader

The key words “MUST”, “MUST NOT”, “REQUIRED”, “SHALL”, “SHALL NOT”, “SHOULD”, “SHOULD NOT”, “RECOMMENDED”, “MAY”, and “OPTIONAL” in this document are to be interpreted as described in RFC 2119.

  • いくつか登場するキーワード(MUST, MUST NOT etc...)は、RFC 2119で説明されてるような解釈とする

内容の構成は章立てされており、いくつかの章では節に当たる項目が用意されてる
引用に関しては全てPSR-1のものである
箇条書きされてる箇所は 和訳+個人の解釈 の入った内容となってる

※以下、学習の一環として自分の解釈とコードを交えた内容を記載しますが、間違いあればコメント頂けますと幸いです。早急に対応しますのでご遠慮なく。

1. 概要

This PSR describes a specification for autoloading classes from file paths. It is fully interoperable, and can be used in addition to any other autoloading specification, including PSR-0. This PSR also describes where to place files that will be autoloaded according to the specification.

  • ここではファイルパスからクラスを自動ロードするための仕様について記述する
  • PSR-0を含む他の自動ロード仕様に加えて使用する事ができる
  • 自動ロードされるファイルの配置場所についても説明する

2. 仕様

1. The term “class” refers to classes, interfaces, traits, and other similar structures.

2. A fully qualified class name has the following form:
\<NamespaceName>(\<SubNamespaceNames>)*\<ClassName>
  1. The fully qualified class name MUST have a top-level namespace name, also known as a “vendor namespace”.
  2. The fully qualified class name MAY have one or more sub-namespace names.
  3. The fully qualified class name MUST have a terminating class name.
  4. Underscores have no special meaning in any portion of the fully qualified class name.
  5. Alphabetic characters in the fully qualified class name MAY be any combination of lower case and upper case.
  6. All class names MUST be referenced in a case-sensitive fashion.

3. When loading a file that corresponds to a fully qualified class name …
  1. A contiguous series of one or more leading namespace and sub-namespace names, not including the leading namespace separator, in the fully qualified class name (a “namespace prefix”) corresponds to at least one “base directory”.
  2. The contiguous sub-namespace names after the “namespace prefix” correspond to a subdirectory within a “base directory”, in which the namespace separators represent directory separators. The subdirectory name MUST match the case of the sub-namespace names.
  3. The terminating class name corresponds to a file name ending in .php. The file name MUST match the case of the terminating class name.

4. Autoloader implementations MUST NOT throw exceptions, MUST NOT raise errors of any level, and SHOULD NOT return a value.

  • 1. 「クラス」という用語は、クラス、インターフェース、特性、およびその他の類似の構造を指す
  • 2. 完全修飾クラス名の形式は次のとおりです。
    • \<NamespaceName>(\<SubNamespaceNames>)*\<ClassName>
    • 1. 「ベンダー名前空間」と呼ばれる最上位の名前空間が必要(NamespaceNameがそれに当たる)
    • 2. 1つ以上のサブ名前空間名が含まれてる場合がある(SubNamespaceNamesがそれに当たる)
    • 3. 終了クラス名が必要(ClassNameがそれに当たる)
    • 4. アンダースコアに特別な意味は持たない
    • 5. 命名は大文字と小文字の組み合わせによって構成する
    • 6. 全てのクラス名は大文字と小文字を区別して参照しなければならない
  • 3. 完全修飾クラス名に対応するファイルをロードするとき
    • 1. 完全修飾クラス名(名前空間接頭辞)の先頭の名前空間区切り文字を含まない1つ以上の先頭の名前空間とサブ名前空間名の連続したシリーズは、少なくとも1つの「ベースディレクトリ」に対応する
    • 2. 「ネームスペースプレフィックス」の後の連続するサブネームスペース名は、「ベースディレクトリ」内のサブディレクトリに対応し、(ネームスペースセパレータはディレクトリセパレータを示す) サブディレクトリ名は、サブ名前空間名の大文字小文字と一致しなければならない
    • 3. 終了クラス名は、.phpで終わるファイル名に対応する、ファイル名は、終了するクラス名の大文字小文字と一致しなければならない

3. 例

The table below shows the corresponding file path for a given fully qualified class name, namespace prefix, and base directory.

FULLY QUALIFIED CLASS NAME NAMESPACE PREFIX BASE DIRECTORY RESULTING FILE PATH
\Acme\Log\Writer\File_Writer Acme\Log\Writer ./acme-log-writer/lib/ ./acme-log-writer/lib/File_Writer.php
\Aura\Web\Response\Status Aura\Web /path/to/aura-web/src/ /path/to/aura-web/src/Response/Status.php
\Symfony\Core\Request Symfony\Core ./vendor/Symfony/Core/ ./vendor/Symfony/Core/Request.php
\Zend\Acl Zend /usr/includes/Zend/ /usr/includes/Zend/Acl.php

For example implementations of autoloaders conforming to the specification, please see the examples file. Example implementations MUST NOT be regarded as part of the specification and MAY change at any time.

  • 表は、特定の完全修飾クラス名名前空間プレフィックス、およびベースディレクトリに対応するファイルパスを示してる
  • 仕様に準拠したオートローダーの実装例については、サンプルファイルを参照してください

注意

  • 冒頭でも述べたように、主に箇条書きされてる箇所については、 和訳+個人の解釈 の入った内容になっているため、参考程度の理解としていただきたい
  • また、表現に誤りがある場合、遠慮なくコメントいただけると幸いです(この内容に関しては表現の誤りは致命的なため)
  • 学習の一環としてアウトプットしたいため投稿したので、その点暖かい目で読んで頂けると嬉しい
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0