1
0
Fork 0

[improve-docs] Mention classmap autoloading in basic-usage

pull/573/head
Igor Wiedler 2012-04-13 12:06:53 +02:00
parent 5269bfa25c
commit a2eeeab79a
2 changed files with 19 additions and 12 deletions

View File

@ -135,16 +135,15 @@ but it makes life quite a bit simpler.
## Autoloading ## Autoloading
For libraries that follow the [PSR-0](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md) For libraries that specify autoload information, Composer generates a
naming standard, Composer generates a `vendor/.composer/autoload.php` file `vendor/.composer/autoload.php` file. You can simply include this file and you
for autoloading. You can simply include this file and you will get autoloading will get autoloading for free.
for free.
require 'vendor/.composer/autoload.php'; require 'vendor/.composer/autoload.php';
This makes it really easy to use third party code: For monolog, it This makes it really easy to use third party code. For example: If your
means that we can just start using classes from it, and they will be project depends on monolog, you can just start using classes from it, and they
autoloaded. will be autoloaded.
$log = new Monolog\Logger('name'); $log = new Monolog\Logger('name');
$log->pushHandler(new Monolog\Handler\StreamHandler('app.log', Monolog\Logger::WARNING)); $log->pushHandler(new Monolog\Handler\StreamHandler('app.log', Monolog\Logger::WARNING));
@ -160,8 +159,12 @@ to `composer.json`.
} }
} }
This is a mapping from namespaces to directories. The `src` directory would be Composer will register a
in your project root. An example filename would be `src/Acme/Foo.php` [PSR-0](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md)
autoloader for the `Acme` namespace.
You define a mapping from namespaces to directories. The `src` directory would
be in your project root. An example filename would be `src/Acme/Foo.php`
containing an `Acme\Foo` class. containing an `Acme\Foo` class.
After adding the `autoload` field, you have to re-run `install` to re-generate After adding the `autoload` field, you have to re-run `install` to re-generate
@ -174,6 +177,10 @@ This can be useful for autoloading classes in a test suite, for example.
$loader = require 'vendor/.composer/autoload.php'; $loader = require 'vendor/.composer/autoload.php';
$loader->add('Acme\Test', __DIR__); $loader->add('Acme\Test', __DIR__);
In addition to PSR-0 autoloading, classmap is also supported. This allows
classes to be autoloaded even if they do not conform to PSR-0. See the
[autoload reference](04-schema.md#autoload) for more details.
> **Note:** Composer provides its own autoloader. If you don't want to use > **Note:** Composer provides its own autoloader. If you don't want to use
that one, you can just include `vendor/.composer/autoload_namespaces.php`, that one, you can just include `vendor/.composer/autoload_namespaces.php`,
which returns an associative array mapping namespaces to directories. which returns an associative array mapping namespaces to directories.

View File

@ -188,7 +188,7 @@ Optional.
Autoload mapping for a PHP autoloader. Autoload mapping for a PHP autoloader.
Currently [PSR-0](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md) Currently [PSR-0](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md)
autoloading and ClassMap generation are supported. autoloading and classmap generation are supported.
Under the `psr-0` key you define a mapping from namespaces to paths, relative to the Under the `psr-0` key you define a mapping from namespaces to paths, relative to the
package root. package root.
@ -212,7 +212,7 @@ you can specify them as an array as such:
} }
You can use the classmap generation support to define autoloading for all libraries You can use the classmap generation support to define autoloading for all libraries
that do not follow "PSR-0". To configure this you specify all directories that do not follow PSR-0. To configure this you specify all directories
to search for classes. to search for classes.
Example: Example: