Download the PHP package parable-php/di without Composer
On this page you can find all versions of the php package parable-php/di. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download parable-php/di
More information about parable-php/di
Files in parable-php/di
Package di
Short Description Parable DI is a micro Dependency Injection Container
License MIT
Homepage https://github.com/parable-php/di
Informations about the package di
Parable DI Container
Parable DI is a no-nonsense dependency injection container that gets the job done.
Install
Php 8.0+ and composer are required.
Usage
Example usage of a straightforward situation:
Example usage of an interface-hinted dependency being mapped:
The above situation can also be solved by instantiating and then storing ClassWithThatInterface
under ThatInterface
.
Example usage of a class that needs the di itself, in case you need to do dynamic DI:
For all other use cases, simply check the tests in tests/ContainerTest.php
.
API
get(string $name): object
- creates or gets instancehas(string $name): bool
- check if instance is storedassertHas(string $name): void
- check and throw exception if instance is not storedbuild(string $name): object
- build instance with stored deps, don't storebuildAll(string $name): object
- build instance with new deps, don't storemap(string $requested, string $replacement): void
- allow pre-emptively defining a replacement class to be instantiated when the requested name is retrieved or built. Use for lazy-loading classes, i.e. for interface deps.unmap(string $requested): void
- removes the mapping and clears any previously mapped instancesgetDependenciesFor(string $name, [int $storedDependencies]): array
- get dependencies for instance, with stored (default) or new depsstore(object $instance, [string $name]): void
- store instance under name, or under instance name by defaultclear(string $name): void
- clear instanceclearExcept(array $keep): void
- clear all except provided names, throws if any of the instances to keep doesn't existclearAll(): void
- clear all
Where object
refers to any instance of any class.
Contributing
Any suggestions, bug reports or general feedback is welcome. Use github issues and pull requests, or find me over at devvoh.com.
License
All Parable components are open-source software, licensed under the MIT license.