Class: ObjectFactory

Class for creation of the objects.

Details

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.

Authors

Oleg Schildt

Package

Source code

 

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.


Property: ObjectFactory::$itable

protected static array $itable = [];

Internal array for storing the mapping between interfaces/classes and the bound classes.

Type

array

Authors

Oleg Schildt

Source code


Property: ObjectFactory::$singletons

protected static array $singletons = [];

Internal array for storing the singleton instances for re-using.

Type

array

Authors

Oleg Schildt

Source code


Method: ObjectFactory::bindClass()

public static function bindClass(object|string $interface_or_class, object|string $class, ?callable $init_function = null ) : void;

Binds a class to an interface or a parent class.

Details

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.

Parameters

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:

function (object $instance) : void;
  • $instance - the created instance.

Example:

ObjectFactory::bindClass(ILanguageManager::class, LanguageManager::class, function($instance) {
  $instance->detectLanguage();
});

ObjectFactory::bindClass(IRecordsetManager::class, RecordsetManager::class, function($instance) {
  $instance->setDBWorker(dbworker());
});

Returns

void

Throws

\Exception

It might throw the following exceptions in the case of any errors:

  • if the interface or bound class is not specified.
  • if the interface or class does not exist.
  • if the bound class is empty.
  • if the bound class does not implement the corresponding interface.
  • if the bound class is not instantiable.
  • if the check of the classes and interfaces fails.

Authors

Oleg Schildt

Source code


Method: ObjectFactory::bindClassContext()

public static function bindClassContext(string $context, object|string $interface_or_class, object|string $class, ?callable $init_function = null ) : void;

Binds a class to an interface or a parent class.

Details

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.

Parameters

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:

function (object $instance) : void;
  • $instance - the created instance.

Example:

ObjectFactory::bindClassContext("console", ILanguageManager::class, LanguageManager::class, function($instance) {
  $instance->detectLanguage();
});

ObjectFactory::bindClassContext("console", IRecordsetManager::class, RecordsetManager::class, function($instance) {
  $instance->setDBWorker(dbworker());
});

Returns

void

Throws

\Exception

It might throw the following exceptions in the case of any errors:

  • if the interface or bound class is not specified.
  • if the interface or class does not exist.
  • if the bound class is empty.
  • if the bound class does not implement the corresponding interface.
  • if the bound class is not instantiable.
  • if the check of the classes and interfaces fails.

Authors

Oleg Schildt

Source code


Method: ObjectFactory::getInstance()

public static function getInstance(object|string $interface_or_class, bool $singleton, string $context = "default" ) : object;

Сreates an object that support the interface $interface_or_class.

Parameters

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

Returns object of the class bound to the interface.

Throws

\Exception

It might throw the following exceptions in the case of any errors:

  • if the interface or class is not specified.
  • if the interface or class does not exist.
  • if the check of the classes and interfaces fails.

Authors

Oleg Schildt

Source code