PSR-4

剛剛看到「Composer now supports PSR-4」才發現有 PSR-4,而且出了好幾個禮拜了...

PSR-4

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 的情況下,設計 PSR-4 讓人感覺有點怪,因為功能跟 PSR-0 是衝突的。

不過仔細看看以後發現 PSR-4 規則比較「乾淨」,有種想要汰換 PSR-0 的想法... 而且 Composercomposer.json 的設計也允許針對不同的 prefix 使用不同的邏輯,看起來是把 PSR-0 當作過渡期的設計,希望大家換到 PSR-4?

PSR-0 與 PSR-4 最明顯的差異是對底線 (underscore) 解讀不同,在 PSR-4 中的底線沒有任何意義:

Underscores have no special meaning in any portion of the fully qualified class name.

在 PSR-0 裡則是說 class name 的底線必須換成 DIRECTORY_SEPARATOR:

Each _ character in the CLASS NAME is converted to a DIRECTORY_SEPARATOR. The _ character has no special meaning in the namespace.

來想看看要怎麼轉換現有的系統...

Leave a Reply

Your email address will not be published. Required fields are marked *