Download the PHP package charcoal/factory without Composer
On this page you can find all versions of the php package charcoal/factory. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download charcoal/factory
More information about charcoal/factory
Files in charcoal/factory
Package factory
Short Description Charcoal object creation (Factory, AbstractFactory, Builder, Class Resolver)
License MIT
Homepage https://charcoal.locomotive.ca
Informations about the package factory
Charcoal Factory
The Factory package provides abstract object factories to create objects.
Installation
Usage
Factories can resolve a type to a FQN and create instance of this class with an optional given set of arguments, while ensuring a default base class.
Factory options should be set directly from the constructor:
Constructor options (class dependencies) are:
Name | Type | Default | 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 | null |
Optional. A callback function that will be called upon object creation. |
resolver |
callable | null [1] |
Optional. A class resolver. If none is provided, a default will be used. |
resolver_options |
array | null |
Optional. Resolver options (prefix, suffix, capitals and replacements). This is ignored / unused if resolver is provided. |
Notes:
- [1] If no resolver is provided, a default
\Charcoal\Factory\GenericResolver
will be used.
Class resolver
The type (class identifier) sent to the create()
method can be parsed / resolved with a custom Callable
resolver.
If no resolver
is passed to the constructor, a default \Charcoal\Factory\GenericResolver
is used. This default resolver transforms, for example, my/custom/foo-bar
into \My\Custom\FooBar
.
Class map and aliases
Class aliases can be added by setting them in the Factory constructor:
Ensuring a type of object
Ensuring a type of object can be done by setting the base_class
property.
The recommended way of setting the base class is by setting it in the constructor:
👉 Note that Interfaces can also be used as a factory's base class.
Setting a default type of object
It is possible to set a default type of object (default class) by setting the default_class
property.
The recommended way of setting the default class is by setting it in the constructor:
⚠️ Setting a default class name changes the standard Factory behavior. When an invalid class name is used, instead of throwing an
Exception
, an object of the default class type will always be returned.
Constructor arguments
It is possible to set "automatic" constructor arguments that will be passed to every created object.
The recommended way of setting constructor arguments is by passing an array of arguments to the constructor:
Executing an object callback
It is possible to execute an object callback upon object instanciation. A callback is any Callable
that takes the newly created object by reference as its function parameter.
The recommended way of adding an object callback is by passing a Callable
to the constructor: