robloach / class-loader-adapter
The Class Loader Adapter provides a common interface to interact with a number of different class loaders.
Requires
- php: >=5.3.3
Requires (Dev)
- composer/composer: 1.*
- phpunit/phpunit: 3.*
- symfony/class-loader: 2.*
Suggests
- composer/composer: Allows interaction with the Composer ClassLoader
- symfony/classloader: Allows interaction with the Symfony ClassLoaders
This package is auto-updated.
Last update: 2024-11-10 09:07:35 UTC
README
Common interface to interact with a number of different class loaders.
Design
It is recommended to just have one class loader interface for your application, and initialize and use it only once during your application's starting point. It can, however, sometimes be handy to allow swapping the class loader while still using the same API to interact with it. Class Loader Adapter aims to provide that interface commonality between a bunch of different third-party class loaders.
Interoperability
- Symfony\Component\ClassLoader\ClassLoader
- Symfony\Component\ClassLoader\MapClassLoader
- Symfony\Component\ClassLoader\UniversalClassLoader
- Composer\Autoload\ClassLoader
Usage
Initialize with whichever class loader you want to use:
// Initialize the loader with the Symfony UniversalClassLoader. $loader = new RobLoach\ClassLoaderAdapter\Symfony\UniversalClassLoader(); // Initialize the loader with the Composer ClassLoader. $loader = new RobLoach\ClassLoaderAdapter\Composer\ClassLoader();
Since all of the Class Loader Adapter loaders use the same ClassLoaderInterface , you can interact with them all the same way.
// Add a namespace prefix. $loader->addPrefix('Foo', 'src/'); // Add a class map. $map = array('Foo\Bar' => 'src/Foo/Bar.php'); $loader->addClassMap($map); // Register the class loader so that the classes can be loaded accordingly. $loader->register();
Although the interface is in place, class loader functionality and support slightly differs from class loader to class loader. Take note of this when switching between the class loaders.