fyre / container
A container library.
v1.0.2
2024-11-02 02:28 UTC
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.59
- fyre/php-cs-fixer-config: ^1.0
- phpunit/phpunit: ^11
README
FyreContainer is a free, open-source container library for PHP.
Table Of Contents
Installation
Using Composer
composer require fyre/container
In PHP:
use Fyre\Container\Container;
Basic Usage
$bind
is a boolean indicating whether to bind the container instance to itself, and will default to true.
$container = new Container($bind);
Methods
Bind
Bind an alias to a factory Closure or class name.
$alias
is a string representing the alias.$factory
is a string representing the class name, or a Closure that returns an instance of the class, and will default to the$alias
.
$container->bind($alias, $factory);
Bind Attribute
Bind a contextual attribute to a handler.
$attribute
is a string representing the attribute.$handler
is a Closure that will resolve a value from the attribute.
$container->bindAttribute($attribute, $handler);
Build
Build a class name, injecting dependencies as required.
$className
is a string representing the class name.$arguments
is an array containing the named arguments for the class constructor.
$instance = $container->build($className, $arguments);
Call
Execute a callable using resolved dependencies.
$callable
is an array, string or object representing the callable.$arguments
is an array containing the named arguments for the callabck.
$result = $container->call($callable, $arguments);
Clear Scoped
Clear the scoped instances.
$container->clearScoped();
Instance
Bind an alias to a class instance.
$alias
is a string representing the alias.$instance
is an object representing the class instance.
$instance = $container->instance($alias, $instance);
Scoped
Bind an alias to a factory Closure or class name as a reusable scoped instance.
$alias
is a string representing the alias.$factory
is a string representing the class name, or a Closure that returns an instance of the class, and will default to the$alias
.
$container->scoped($alias, $factory);
Singleton
Bind an alias to a factory Closure or class name as a reusable instance.
$alias
is a string representing the alias.$factory
is a string representing the class name, or a Closure that returns an instance of the class, and will default to the$alias
.
$container->singleton($alias, $factory);
Use
Use an instance of a class.
$alias
is a string representing the alias.$arguments
is an array containing the named arguments for the class constructor.
$instance = $container->use($alias, $arguments);
Static Methods
Get Instance
Get the global instance.
$container = Container::getInstance();
Set Instance
Set the global instance.
$container
is a Container.
Container::setInstance($container);