$resolved
$resolved : array
The Map Factory resolves the **class name** from an associative array with the **type** key.
Name | Type | Description |
---|---|---|
base_class |
string | Optional. A base class (or interface) to ensure a type of object. |
default_class |
string | Optional. A default class, as fallback when the requested object is not resolvable. |
arguments |
array | Optional. Constructor arguments that will be passed along to created instances. |
callback |
Callable | Optional. A callback function that will be called upon object creation. |
resolver |
Callable | Optional. A class resolver. If none is provided, a default will be used. |
resolver_options |
array | Optional. Resolver options (prefix, suffix, capitals and replacements). This is ignored / unused if resolver is provided. |
create(string $type, array $args = null, callable $cb = null) : mixed
Create a new instance of a class, by type.
Unlike get()
, this method always return a new instance of the requested class.
It is possible to pass a callback method that will be executed upon object instanciation.
The callable should have a signature: function($obj);
where $obj is the newly created object.
string | $type | The type (class ident). |
array | $args | Optional. Constructor arguments (will override the arguments set on the class from constructor). |
callable | $cb | Optional. Object callback, called at creation. Will run in addition to the default callback, if any. |
If the base class is set and the resulting instance is not of the base class.
If type argument is not a string or is not an available type.
The instance / object
get(string $type, array $args = null) : mixed
Get (load or create) an instance of a class, by type.
Unlike create()
(which always call a new
instance), this function first tries to load / reuse
an already created object of this type, from memory.
string | $type | The type (class ident). |
array | $args | The constructor arguments (optional). |
If type argument is not a string.
The instance / object
setBaseClass(string $type) : \Charcoal\Factory\FactoryInterface
If a base class is set, then it must be ensured that the created objects are `instanceof` this base class.
string | $type | The FQN of the class, or "type" of object, to set as base class. |
If the class is not a string or is not an existing class / interface.
Chainable
setDefaultClass(string $type) : \Charcoal\Factory\FactoryInterface
If a default class is set, then calling `get()` or `create()` an invalid type should return an object of this class instead of throwing an error.
string | $type | The FQN of the class, or "type" of object, to set as default class. |
If the class name is not a string or not a valid class.
Chainable
setArguments(array $arguments) : \Charcoal\Factory\FactoryInterface
array | $arguments | The constructor arguments to be passed to the created object's initialization. |
Chainable
setCallback(callable $callback) : \Charcoal\Factory\FactoryInterface
callable | $callback | The object callback. |
Chainable
isResolvable(string $type) : boolean
Wether a `type` is resolvable. The Generic Factory simply checks if the _FQN_ `type` class exists.
string | $type | The "type" of object to resolve (the object ident). |
If the type parameter is not a string.
createClass(string $classname, mixed $args) : mixed
Create a class instance with given arguments.
How the constructor arguments are passed depends on its type:
string | $classname | The FQN of the class to instanciate. |
mixed | $args | The constructor arguments. |
The created object.
addClassToMap(string $type, string $className) : \Charcoal\Factory\FactoryInterface
Add a class name to the available types _map_.
string | $type | The type (class ident). |
string | $className | The FQN of the class. |
If the $type parameter is not a striing or the $className class does not exist.
Chainable
setResolver(callable $resolver) : \Charcoal\Factory\FactoryInterface
callable | $resolver | The class resolver instance to use. |
Chainable
setMap(array<mixed,string> $map) : \Charcoal\Factory\FactoryInterface
Add multiple types, in a an array of `type` => `className`.
array<mixed,string> | $map | The map (key=>classname) to use. |
Chainable