Class for creation of the objects.
The class ObjectFactory is an auxiliary class that empowers the factory methods. It provides the methods for binding of class implementations to the interfaces and for creation of objects for the requested interface.
| Properties | |
|---|---|
|
static
protected
|
Internal array for storing the mapping between interfaces/classes and the bound classes. |
|
static
protected
|
Internal array for storing the singleton instances for re-using. |
| Methods | |
|---|---|
|
static
public
|
Binds a class to an interface or a parent class. |
|
static
public
|
Binds a class to an interface or a parent class. |
|
static
public
|
Сreates an object that support the interface $interface_or_class. |
Internal array for storing the mapping between interfaces/classes and the bound classes.
Internal array for storing the singleton instances for re-using.
Binds a class to an interface or a parent class.
The key point of this approach is the definition common interfaces and implementation of them in classes. When an object that supports an interface is requested, an instance of the corresponding bound class is created. When you want to change the class, you need just bind the new class to the interface.
You can also bind a class to itself and request it by own name in factory method. And later, you can implement a derived class and change the binding without affecting the code in the business logic.
| Name | Pass type | Value type | Default value | Description |
|---|---|---|---|---|
| $interface_or_class | by value | object|string |
Name of the class/interface as string or the class/interface. |
|
| $class | by value | object|string |
The class which instance should be created if the object is requested. |
|
| $init_function | by value | ?callable | null |
The optional initialization function. You can provide it to do some custom initialization. The signature of this function is:
Example:
|
It might throw the following exceptions in the case of any errors:
Binds a class to an interface or a parent class.
The key point of this approach is the definition common interfaces and implementation of them in classes. When an object that supports an interface is requested, an instance of the corresponding bound class is created. When you want to change the class, you need just bind the new class to the interface.
You can also bind a class to itself and request it by own name in factory method. And later, you can implement a derived class and change the binding without affecting the code in the business logic.
| Name | Pass type | Value type | Default value | Description |
|---|---|---|---|---|
| $context | by value | string |
The context of binding. You can introduce a different context, and bind another implementation. Then, you can request another instance specifying the context explicitly. |
|
| $interface_or_class | by value | object|string |
Name of the class/interface as string or the class/interface. |
|
| $class | by value | object|string |
The class which instance should be created if the object is requested. |
|
| $init_function | by value | ?callable | null |
The optional initialization function. You can provide it to do some custom initialization. The signature of this function is:
Example:
|
It might throw the following exceptions in the case of any errors:
Сreates an object that support the interface $interface_or_class.
| Name | Pass type | Value type | Default value | Description |
|---|---|---|---|---|
| $interface_or_class | by value | object|string |
Name of the class/interface which instance should be created. |
|
| $singleton | by value | bool |
If the parameter is true, it ensures that only one instance of this object exists. The singleton is a usual patter for the action objects like SessionManager, EventManager, DBWorker etc. It makes no sense to produce many instances of such classes, it wastes the computer resources and might cause errors. |
|
| $context | by value | string | "default" |
The context of binding. You can introduce a different context, and bind another implementation. Then, you can request another instance specifying the context explicitly. |
Returns object of the class bound to the interface.
It might throw the following exceptions in the case of any errors: