Documentation

Micro
in package

Micro PHP Dependency Injection

Table of Contents

$app  : AbstractApp|null
Holds the instance of the application
$classes  : array<string|int, mixed>
Stores the classes in [interface => class] format, the class can be null
$instances  : array<string|int, mixed>
Stores the instances in [interface => instance] format
add()  : void
Adds a class for an interface
app()  : AbstractApp|null
Returns the instance of the application
create()  : object
Creates an instance for the given class
get()  : object
Creates the singleton instance for the given interface, stores it in `$instances`, then returns with it
getCallable()  : mixed
Creates an instance of the callable if needed, then returns with it
getClass()  : string
Returns with the class for the given interface
hasInterface()  : bool
interfaces()  : array<string|int, mixed>
Returns with all 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

$app

Holds the instance of the application

protected static AbstractApp|null $app = null

$classes

Stores the classes in [interface => class] format, the class can be null

protected static array<string|int, mixed> $classes = []

$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[, string|null $class = null ]) : void

For example:

Micro::add(ConfigInterface::class, Config::class);

or

Micro::add(Config::class);
Parameters
$interface : string
$class : string|null = null
Return values
void

create()

Creates an instance for the given class

public static create(string $class[, array<string|int, mixed> $parameters = [] ][, array<string|int, mixed> $dependencyStack = [] ]) : object

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 App extends AbstractApp {
  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
$parameters : array<string|int, mixed> = []
$dependencyStack : array<string|int, mixed> = []
Tags
throws
MicroException
Return values
object

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 = [] ]) : object

It returns instantly if the instance was stored before.

Parameters
$interface : string
$parameters : array<string|int, mixed> = []
$dependencyStack : array<string|int, mixed> = []
Tags
throws
MicroException
Return values
object

getCallable()

Creates an instance of the callable if needed, then returns with it

public static getCallable(mixed $callable) : mixed
Parameters
$callable : mixed
Tags
throws
MicroException
Return values
mixed

getClass()

Returns with the class for the given interface

public static getClass(string $interface) : string
Parameters
$interface : string
Tags
throws
MicroException

If the interface wasn't added

Return values
string

hasInterface()

public static hasInterface(string $interface) : bool
Parameters
$interface : string
Return values
bool

interfaces()

Returns with all the interfaces in an array

public static interfaces() : array<string|int, mixed>
Return values
array<string|int, mixed>

isMicroCallable()

Returns true if the `$callable` is a Micro Framework callable

public static isMicroCallable(mixed $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
$callable : mixed
Return values
bool

run()

Sets the application instance and runs it

public static run(AbstractApp $app) : void

First it sets the instance, then calls the fullInit() and fullProcess() methods of the $app.

Parameters
$app : AbstractApp
Tags
throws
MicroException

if the instance was set before

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
$reflectionClass : ReflectionClass
$dependencyStack : array<string|int, mixed> = []
Tags
throws
MicroException
Return values
array<string|int, mixed>

Search results