|
1 | 1 | ClassLoader Component
|
2 | 2 | =====================
|
3 | 3 |
|
4 |
| -ClassLoader loads your project classes automatically if they follow some |
5 |
| -standard PHP conventions. |
6 |
| - |
7 |
| -The ClassLoader object is able to autoload classes that implement the PSR-0 |
8 |
| -standard or the PEAR naming convention. |
9 |
| - |
10 |
| -First, register the autoloader: |
11 |
| - |
12 |
| -```php |
13 |
| -require_once __DIR__.'/src/Symfony/Component/ClassLoader/ClassLoader.php'; |
14 |
| - |
15 |
| -use Symfony\Component\ClassLoader\ClassLoader; |
16 |
| - |
17 |
| -$loader = new ClassLoader(); |
18 |
| -$loader->register(); |
19 |
| -``` |
20 |
| - |
21 |
| -Then, register some namespaces with the `addPrefix()` method: |
22 |
| - |
23 |
| -```php |
24 |
| -$loader->addPrefix('Symfony', __DIR__.'/src'); |
25 |
| -$loader->addPrefix('Monolog', __DIR__.'/vendor/monolog/src'); |
26 |
| -``` |
27 |
| - |
28 |
| -The `addPrefix()` method takes a namespace prefix and a path where to look for |
29 |
| -the classes as arguments. |
30 |
| - |
31 |
| -You can also register a sub-namespaces: |
32 |
| - |
33 |
| -```php |
34 |
| -$loader->addPrefix('Doctrine\\Common', __DIR__.'/vendor/doctrine-common/lib'); |
35 |
| -``` |
36 |
| - |
37 |
| -The order of registration is significant and the first registered namespace |
38 |
| -takes precedence over later registered one. |
39 |
| - |
40 |
| -You can also register more than one path for a given namespace: |
41 |
| - |
42 |
| -```php |
43 |
| -$loader->addPrefix('Symfony', array(__DIR__.'/src', __DIR__.'/symfony/src')); |
44 |
| -``` |
45 |
| - |
46 |
| -Alternatively, you can use the `addPrefixes()` method to register more |
47 |
| -than one namespace at once: |
48 |
| - |
49 |
| -```php |
50 |
| -$loader->addPrefixes(array( |
51 |
| - 'Symfony' => array(__DIR__.'/src', __DIR__.'/symfony/src'), |
52 |
| - 'Doctrine\\Common' => __DIR__.'/vendor/doctrine-common/lib', |
53 |
| - 'Doctrine' => __DIR__.'/vendor/doctrine/lib', |
54 |
| - 'Monolog' => __DIR__.'/vendor/monolog/src', |
55 |
| -)); |
56 |
| -``` |
57 |
| - |
58 |
| -For better performance, you can use the APC class loader: |
59 |
| - |
60 |
| -```php |
61 |
| -require_once __DIR__.'/src/Symfony/Component/ClassLoader/ClassLoader.php'; |
62 |
| -require_once __DIR__.'/src/Symfony/Component/ClassLoader/ApcClassLoader.php'; |
63 |
| - |
64 |
| -use Symfony\Component\ClassLoader\ClassLoader; |
65 |
| -use Symfony\Component\ClassLoader\ApcClassLoader; |
66 |
| - |
67 |
| -$loader = new ClassLoader(); |
68 |
| -$loader->addPrefix('Symfony', __DIR__.'/src'); |
69 |
| - |
70 |
| -$loader = new ApcClassLoader('apc.prefix.', $loader); |
71 |
| -$loader->register(); |
72 |
| -``` |
73 |
| - |
74 |
| -Furthermore, the component provides tools to aggregate classes into a single |
75 |
| -file, which is especially useful to improve performance on servers that do not |
76 |
| -provide byte caches. |
| 4 | +The ClassLoader component provides tools to autoload your classes and cache |
| 5 | +their locations for performance. |
77 | 6 |
|
78 | 7 | Resources
|
79 | 8 | ---------
|
80 | 9 |
|
81 |
| -You can run the unit tests with the following command: |
82 |
| - |
83 |
| - $ cd path/to/Symfony/Component/ClassLoader/ |
84 |
| - $ composer install |
85 |
| - $ phpunit |
| 10 | + * [Documentation](https://symfony.com/doc/current/components/class_loader/index.html) |
| 11 | + * [Contributing](https://symfony.com/doc/current/contributing/index.html) |
| 12 | + * [Report issues](https://github.com/symfony/symfony/issues) and |
| 13 | + [send Pull Requests](https://github.com/symfony/symfony/pulls) |
| 14 | + in the [main Symfony repository](https://github.com/symfony/symfony) |