Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 84 lines (64 sloc) 3.033 kb
cf76304 @leofeyer Amend the missing PSR-0 headline
leofeyer authored
1 Autoloading Standard
2 ====================
3
61f2d25 @philsturgeon Mark PSR-0 as Deprecated.
philsturgeon authored
4 > **Deprecated** - As of 2014-10-21 PSR-0 has been marked as deprecated. [PSR-4] is now recommended
5 as an alternative.
6
7 [PSR-4]: http://www.php-fig.org/psr/psr-4/
8
ab3b658 @weierophinney Initial creation of PHP FIG repo
weierophinney authored
9 The following describes the mandatory requirements that must be adhered
10 to for autoloader interoperability.
11
0ac3a82 @artnez fixed markdown formatting
artnez authored
12 Mandatory
13 ---------
14
ab3b658 @weierophinney Initial creation of PHP FIG repo
weierophinney authored
15 * A fully-qualified namespace and class must have the following
0ac3a82 @artnez fixed markdown formatting
artnez authored
16 structure `\<Vendor Name>\(<Namespace>\)*<Class Name>`
ab3b658 @weierophinney Initial creation of PHP FIG repo
weierophinney authored
17 * Each namespace must have a top-level namespace ("Vendor Name").
18 * Each namespace can have as many sub-namespaces as it wishes.
0ac3a82 @artnez fixed markdown formatting
artnez authored
19 * Each namespace separator is converted to a `DIRECTORY_SEPARATOR` when
ab3b658 @weierophinney Initial creation of PHP FIG repo
weierophinney authored
20 loading from the file system.
059a804 @localheinz Fix markdown
localheinz authored
21 * Each `_` character in the CLASS NAME is converted to a
22 `DIRECTORY_SEPARATOR`. The `_` character has no special meaning in the
ab3b658 @weierophinney Initial creation of PHP FIG repo
weierophinney authored
23 namespace.
059a804 @localheinz Fix markdown
localheinz authored
24 * The fully-qualified namespace and class is suffixed with `.php` when
ab3b658 @weierophinney Initial creation of PHP FIG repo
weierophinney authored
25 loading from the file system.
26 * Alphabetic characters in vendor names, namespaces, and class names may
27 be of any combination of lower case and upper case.
28
0ac3a82 @artnez fixed markdown formatting
artnez authored
29 Examples
30 --------
31
32 * `\Doctrine\Common\IsolatedClassLoader` => `/path/to/project/lib/vendor/Doctrine/Common/IsolatedClassLoader.php`
33 * `\Symfony\Core\Request` => `/path/to/project/lib/vendor/Symfony/Core/Request.php`
34 * `\Zend\Acl` => `/path/to/project/lib/vendor/Zend/Acl.php`
35 * `\Zend\Mail\Message` => `/path/to/project/lib/vendor/Zend/Mail/Message.php`
ab3b658 @weierophinney Initial creation of PHP FIG repo
weierophinney authored
36
0ac3a82 @artnez fixed markdown formatting
artnez authored
37 Underscores in Namespaces and Class Names
38 -----------------------------------------
39
40 * `\namespace\package\Class_Name` => `/path/to/project/lib/vendor/namespace/package/Class/Name.php`
41 * `\namespace\package_name\Class_Name` => `/path/to/project/lib/vendor/namespace/package_name/Class/Name.php`
ab3b658 @weierophinney Initial creation of PHP FIG repo
weierophinney authored
42
43 The standards we set here should be the lowest common denominator for
44 painless autoloader interoperability. You can test that you are
45 following these standards by utilizing this sample SplClassLoader
46 implementation which is able to load PHP 5.3 classes.
47
0ac3a82 @artnez fixed markdown formatting
artnez authored
48 Example Implementation
49 ----------------------
ab3b658 @weierophinney Initial creation of PHP FIG repo
weierophinney authored
50
51 Below is an example function to simply demonstrate how the above
52 proposed standards are autoloaded.
9408590 @bobthecow Fix markdown formatting for fenced code block
bobthecow authored
53
2776469 @Marlinc Changed the PSR-0 example to a PHP code block
Marlinc authored
54 ```php
55 <?php
ab3b658 @weierophinney Initial creation of PHP FIG repo
weierophinney authored
56
2776469 @Marlinc Changed the PSR-0 example to a PHP code block
Marlinc authored
57 function autoload($className)
58 {
59 $className = ltrim($className, '\\');
60 $fileName = '';
61 $namespace = '';
5f80e82 @donatj Update accepted/PSR-0.md
donatj authored
62 if ($lastNsPos = strrpos($className, '\\')) {
2776469 @Marlinc Changed the PSR-0 example to a PHP code block
Marlinc authored
63 $namespace = substr($className, 0, $lastNsPos);
64 $className = substr($className, $lastNsPos + 1);
65 $fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
ab3b658 @weierophinney Initial creation of PHP FIG repo
weierophinney authored
66 }
2776469 @Marlinc Changed the PSR-0 example to a PHP code block
Marlinc authored
67 $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
68
69 require $fileName;
70 }
6db666f @shubaivan added-spl-autoload-register-for-completed-autoload-example
shubaivan authored
71 spl_autoload_register('autoload');
2776469 @Marlinc Changed the PSR-0 example to a PHP code block
Marlinc authored
72 ```
ab3b658 @weierophinney Initial creation of PHP FIG repo
weierophinney authored
73
0ac3a82 @artnez fixed markdown formatting
artnez authored
74 SplClassLoader Implementation
75 -----------------------------
ab3b658 @weierophinney Initial creation of PHP FIG repo
weierophinney authored
76
77 The following gist is a sample SplClassLoader implementation that can
78 load your classes if you follow the autoloader interoperability
79 standards proposed above. It is the current recommended way to load PHP
80 5.3 classes that follow these standards.
81
29c48bc @weierophinney Fixed bad link markup
weierophinney authored
82 * [http://gist.github.com/221634](http://gist.github.com/221634)
0ac3a82 @artnez fixed markdown formatting
artnez authored
83
Something went wrong with that request. Please try again.