Skip to content

Instantly share code, notes, and snippets.

@simensen
Last active December 26, 2015 06:19
Show Gist options
  • Save simensen/d303dbb6e3f83fefb073 to your computer and use it in GitHub Desktop.
Save simensen/d303dbb6e3f83fefb073 to your computer and use it in GitHub Desktop.

PSR-4: Class Autoloading

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.

  1. Introduction

The goal for this specification is to extend PSR-0 and to provide an alternative option for applications to determine the location of a file resource on a medium as supported by PHP, when a fully-qualified class name is provided.

  1. Scope

This specification is partly backwards incompatible with PSR-0. Where a conflict occurs the rules in this specification precede, or override, the rules in PSR-0.

  1. Specification

  • The _ character has no special meaning in class names.
  • For a collection of one or more classes a namespace prefix MUST be documented.
  • The documented namespace prefix MUST be a valid namespace for each class in the collection.
  • The portion of the fully-qualified class name following the namespace prefix must be considered the fully-qualified class name for the purpose of transformation into a filename.
  • A conforming autoloader MUST NOT interfere with other autoloaders, and as such it MUST NOT throw exceptions, MUST NOT raise errors of any level, and SHOULD NOT return a value.
  1. Examples

  • Given a namespace prefix \Doctrine\Common and a fully-qualified class name \Doctrine\Common\IsolatedClassLoader, the resulting filename would be IsolatedClassLoader.php.
  • Given a namespace prefix \Doctrine and a fully-qualified class name \Doctrine\Common\IsolatedClassLoader, the resulting filename would be Common/IsolatedClassLoader.php.
  • Given a namespace prefix \Symfony\Component\HttpFoundation and a fully-qualified class name \Symfony\Component\HttpFoundation\Request, the resulting filename would be Request.php.
  • Given a namespace prefix \Symfony and a fully-qualified class name \Symfony\Component\HttpFoundation\Request, the resulting filename would be Component/HttpFoundation/Request.php.
  • Given a namespace prefix \Zend and a fully-qualified class name \Zend\Acl, the resulting filename would be Acl.php.
  • Given a namespace prefix \Acme\My_Project and a fully-qualified class name \Acme\My_Project\Some_module\some_class, the resulting filename would be Some_module/some_class.php.
@pmjones
Copy link

pmjones commented Oct 24, 2013

With a nod to @simensen I present this alternative rewrite of the alternative rewrite: https://gist.github.com/pmjones/7129733

Copy link

ghost commented Oct 24, 2013

@philsturgeon - the examples here are more like test cases. I believe they serve to illustrate expected input/output.

I don't know how anyone could not understand the rules. Clearly everyone got PSR0 without any misunderstanding so this document is just PSR0 with a few amendments. PSR0 is already understood, so therefore amendment is simple to follow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment