Skip to content
This repository
Newer
Older
100644 71 lines (56 sloc) 2.848 kb
ab3b658e » weierophinney
2010-11-04 Initial creation of PHP FIG repo
1 The following describes the mandatory requirements that must be adhered
2 to for autoloader interoperability.
3
0ac3a820 » artnez
2012-01-05 fixed markdown formatting
4 Mandatory
5 ---------
6
ab3b658e » weierophinney
2010-11-04 Initial creation of PHP FIG repo
7 * A fully-qualified namespace and class must have the following
0ac3a820 » artnez
2012-01-05 fixed markdown formatting
8 structure `\<Vendor Name>\(<Namespace>\)*<Class Name>`
ab3b658e » weierophinney
2010-11-04 Initial creation of PHP FIG repo
9 * Each namespace must have a top-level namespace ("Vendor Name").
10 * Each namespace can have as many sub-namespaces as it wishes.
0ac3a820 » artnez
2012-01-05 fixed markdown formatting
11 * Each namespace separator is converted to a `DIRECTORY_SEPARATOR` when
ab3b658e » weierophinney
2010-11-04 Initial creation of PHP FIG repo
12 loading from the file system.
5fe60373 » ktomk
2010-11-19 Typo - Backslash needs escaping with markdown to not trigger unwanted…
13 * Each "\_" character in the CLASS NAME is converted to a
0ac3a820 » artnez
2012-01-05 fixed markdown formatting
14 `DIRECTORY_SEPARATOR`. The "\_" character has no special meaning in the
ab3b658e » weierophinney
2010-11-04 Initial creation of PHP FIG repo
15 namespace.
16 * The fully-qualified namespace and class is suffixed with ".php" when
17 loading from the file system.
18 * Alphabetic characters in vendor names, namespaces, and class names may
19 be of any combination of lower case and upper case.
20
0ac3a820 » artnez
2012-01-05 fixed markdown formatting
21 Examples
22 --------
23
24 * `\Doctrine\Common\IsolatedClassLoader` => `/path/to/project/lib/vendor/Doctrine/Common/IsolatedClassLoader.php`
25 * `\Symfony\Core\Request` => `/path/to/project/lib/vendor/Symfony/Core/Request.php`
26 * `\Zend\Acl` => `/path/to/project/lib/vendor/Zend/Acl.php`
27 * `\Zend\Mail\Message` => `/path/to/project/lib/vendor/Zend/Mail/Message.php`
ab3b658e » weierophinney
2010-11-04 Initial creation of PHP FIG repo
28
0ac3a820 » artnez
2012-01-05 fixed markdown formatting
29 Underscores in Namespaces and Class Names
30 -----------------------------------------
31
32 * `\namespace\package\Class_Name` => `/path/to/project/lib/vendor/namespace/package/Class/Name.php`
33 * `\namespace\package_name\Class_Name` => `/path/to/project/lib/vendor/namespace/package_name/Class/Name.php`
ab3b658e » weierophinney
2010-11-04 Initial creation of PHP FIG repo
34
35 The standards we set here should be the lowest common denominator for
36 painless autoloader interoperability. You can test that you are
37 following these standards by utilizing this sample SplClassLoader
38 implementation which is able to load PHP 5.3 classes.
39
0ac3a820 » artnez
2012-01-05 fixed markdown formatting
40 Example Implementation
41 ----------------------
ab3b658e » weierophinney
2010-11-04 Initial creation of PHP FIG repo
42
43 Below is an example function to simply demonstrate how the above
44 proposed standards are autoloaded.
45
46 <?php
47
48 function autoload($className)
49 {
50 $className = ltrim($className, '\\');
51 $fileName = '';
52 $namespace = '';
53 if ($lastNsPos = strripos($className, '\\')) {
54 $namespace = substr($className, 0, $lastNsPos);
55 $className = substr($className, $lastNsPos + 1);
56 $fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
57 }
58 $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
59
60 require $fileName;
61 }
62
0ac3a820 » artnez
2012-01-05 fixed markdown formatting
63 SplClassLoader Implementation
64 -----------------------------
ab3b658e » weierophinney
2010-11-04 Initial creation of PHP FIG repo
65
66 The following gist is a sample SplClassLoader implementation that can
67 load your classes if you follow the autoloader interoperability
68 standards proposed above. It is the current recommended way to load PHP
69 5.3 classes that follow these standards.
70
29c48bc4 » weierophinney
2010-11-04 Fixed bad link markup
71 * [http://gist.github.com/221634](http://gist.github.com/221634)
0ac3a820 » artnez
2012-01-05 fixed markdown formatting
72
Something went wrong with that request. Please try again.