Micro
in package
Micro PHP Dependency Injection
Table of Contents
- $classes : array<string|int, mixed>
- Stores the classes in [interface => class] format, the class can be null
- $instance : App
- Holds the instance of the application
- $instances : array<string|int, mixed>
- Stores the instances in [interface => instance] format
- add() : mixed
- Adds a class for an interface
- create() : mixed
- Creates an instance for the given interface
- get() : mixed
- Creates the singleton instance for the given interface, stores it in `$instances`, then returns with it
- getCallable() : mixed
- Creates and instance of the callable if needed, then returns with it
- getClass() : string
- Returns with the class for the given interface
- hasInterface() : bool
- instance() : mixed
- Returns the instance of the application
- interfaces() : array<string|int, mixed>
- Returns with all of the interfaces in an array
- isMicroCallable() : bool
- Returns true if the `$callable` is a Micro Framework callable
- run() : void
- Sets the application instance and runs it
- createDependencies() : array<string|int, mixed>
- Creates the singleton dependencies for a given class and returns with it as an array
Properties
$classes
Stores the classes in [interface => class] format, the class can be null
protected
static array<string|int, mixed>
$classes
= []
$instance
Holds the instance of the application
protected
static App
$instance
$instances
Stores the instances in [interface => instance] format
protected
static array<string|int, mixed>
$instances
= []
Methods
add()
Adds a class for an interface
public
static add(string $interface[, null $class = null ]) : mixed
For example:
Micro::add(ConfigInterface::class, Config::class);
or
Micro::add(Config::class);
Parameters
- $interface : string
-
The interface
- $class : null = null
-
The class, it can be null, then the interface itself a class
Return values
mixed —create()
Creates an instance for the given interface
public
static create(string $class[, array<string|int, mixed> $parameters = [] ][, array<string|int, mixed> $dependencyStack = [] ]) : mixed
In the following example, the Something
class constructor will get the Config
instance
and the 'someParameterValue' in the $someParameter
.
use Dynart\Micro\Micro; use Dynart\Micro\App; use Dynart\Micro\Config; class Something { private $someParameter; public function __construct(Config $config, $someParameter) { $this->someParameter = $someParameter; } public function someParameter() { return $this->someParameter; } } class MyApp extends App { private $something; public function __construct() { Micro::add(Config::class); Micro::add(Something::class); } public function init() { $this->something = Micro::create(Something::class, ['someParameterValue']); } public function process() { echo $this->something->someParameter(); } }
If the class has a postConstruct()
method it will be called after creation. It can be used for lazy injection.
Parameters
- $class : string
-
The name of the class
- $parameters : array<string|int, mixed> = []
-
Parameters for the constructor. Important: only the parameters that are not injected!
- $dependencyStack : array<string|int, mixed> = []
Return values
mixed —get()
Creates the singleton instance for the given interface, stores it in `$instances`, then returns with it
public
static get(string $interface[, array<string|int, mixed> $parameters = [] ][, array<string|int, mixed> $dependencyStack = [] ]) : mixed
It returns instantly if the instance was stored before.
Parameters
- $interface : string
-
The interface
- $parameters : array<string|int, mixed> = []
-
The parameters for the constructor. Important: only the parameters that are not injected!
- $dependencyStack : array<string|int, mixed> = []
Return values
mixed —getCallable()
Creates and instance of the callable if needed, then returns with it
public
static getCallable( $callable) : mixed
Parameters
Return values
mixed —getClass()
Returns with the class for the given interface
public
static getClass(string $interface) : string
Parameters
- $interface : string
-
The interface
Tags
Return values
string —The class for the interface
hasInterface()
public
static hasInterface(string $interface) : bool
Parameters
- $interface : string
Return values
bool —Is the interface was added?
instance()
Returns the instance of the application
public
static instance() : mixed
Return values
mixed —The instance of the application
interfaces()
Returns with all of the interfaces in an array
public
static interfaces() : array<string|int, mixed>
Return values
array<string|int, mixed> —All of the added interfaces
isMicroCallable()
Returns true if the `$callable` is a Micro Framework callable
public
static isMicroCallable( $callable) : bool
Micro Framework callable means: an array with two strings. The first one is the class name, the second is the method name.
Example:
[Something::class, 'theMethodName']
Parameters
Return values
bool —run()
Sets the application instance and runs it
public
static run(App $app) : void
First it sets the instance, then calls the fullInit()
and fullProcess()
methods of the $app
.
Parameters
- $app : App
-
The application for init and process
Tags
Return values
void —createDependencies()
Creates the singleton dependencies for a given class and returns with it as an array
private
static createDependencies(string $class, ReflectionClass $reflectionClass[, array<string|int, mixed> $dependencyStack = [] ]) : array<string|int, mixed>
Parameters
- $class : string
-
The class name
- $reflectionClass : ReflectionClass
- $dependencyStack : array<string|int, mixed> = []
Return values
array<string|int, mixed> —The created singleton instances