Skip to content

Internals Object ObjectFactory

Stefan Crkvenjakov edited this page Oct 7, 2026 · 1 revision

object\ObjectFactory

Framework Internals · Subsystem overview · Developer guide

Source: awt_src/classes/object/ObjectFactory.php · Snapshot 8411f4287706.

ObjectFactory configures lazy instances by file/class, constructor arguments, properties and ordered method calls. ObjectHandler chooses a concrete class declared by a file.

Imports and dependencies

use model\Model;
use ReflectionClass;
use ReflectionException;

ObjectFactory

Declaration and architectural role

class ObjectFactory

Source description (behavioral corrections are recorded in Known discrepancies):

A factory class used to dynamically create objects and configure them with specified properties, methods, and constructor arguments before returning.

Constants, enum cases, traits and properties

Declarations below preserve visibility, type, default and modifiers exactly. A typed property without an initializer remains uninitialized until its constructor or another method assigns it. Mutable fields belong to this instance unless marked static; readonly fields can be initialized once. Constructor-promoted properties are also listed below.

Member Declaration Purpose or source contract
$classPath private ?string $classPath; Used by __construct(), setClassPath(), setProperty(), callMethods(), create(), __destruct().
$className private ?string $className; Used by __construct(), setClassName(), create(), __destruct().
$type public ?string $type; Used by __construct(), setType(), create(), __destruct().
$constructorArgs private array $constructorArgs; Used by __construct(), setConstructorArgs(), create(), __destruct().
$methodCalls private array $methodCalls; Used by __construct(), setMethodCalls(), addMethodCall(), callMethods(), __destruct().
$methodArgs private array $methodArgs; Used by __construct(), setMethodArgs(), addMethodArgs(), callMethods(), __destruct().
$properties private array $properties; Used by __construct(), setProperties(), addProperty(), setProperty(), __destruct().

Method index

Method Signature
__construct public function __construct()
setClassPath public function setClassPath(string $classPath): self
setType public function setType(string $type): self
setConstructorArgs public function setConstructorArgs(array $constructorArgs): self
setProperties public function setProperties(array $properties): self
addProperty public function addProperty(string $name, string $value): self
setMethodCalls public function setMethodCalls(array $methodCalls): self
setMethodArgs public function setMethodArgs(array $methodArgs): self
setProperty protected function setProperty(object $object): object
addMethodCall public function addMethodCall(string $name): self
addMethodArgs public function addMethodArgs(string $method, array $args): self
setClassName public function setClassName(string $class): self
checkType public function checkType(object $object, string $type): bool
callMethods protected function callMethods(object $object): object
create public function create(): ?object
__destruct public function __destruct()

__construct

Source line 21

public function __construct()

State writes: $this->classPath, $this->className, $this->type, $this->constructorArgs, $this->methodCalls, $this->methodArgs, $this->properties.

Implementation, validation guards and interaction order
public function __construct()
    {
        $this->classPath = null;
        $this->className = null;
        $this->type = null;
        $this->constructorArgs = [];
        $this->methodCalls = [];
        $this->methodArgs = [];
        $this->properties = [];
    }

setClassPath

Source line 39

public function setClassPath(string $classPath): self

Parameters

Parameter Type, modifiers and default Purpose
$classPath string $classPath * @return self

Sets a path to the class which will be initialized.

@param string $classPath @return self @throws \Exception if file does not exist.

State writes: $this->classPath.

Return expressions (including local callbacks): $this.

Explicit exceptions: \Exception. The guards in the implementation below show when each is raised; exceptions from collaborators also propagate unless caught.

Implementation, validation guards and interaction order
/**
     * Sets a path to the class which will be initialized.
     *
     * @param string $classPath
     * @return self
     * @throws \Exception if file does not exist.
     */
    public function setClassPath(string $classPath): self
    {
        if (!file_exists($classPath))
            throw new \Exception("File $classPath does not exist");


        $this->classPath = $classPath;
        return $this;
    }

setType

Source line 56

public function setType(string $type): self

Parameters

Parameter Type, modifiers and default Purpose
$type string $type * @return self

Sets type of the given class for stricter checking.

@param string $type @return self

State writes: $this->type.

Return expressions (including local callbacks): $this.

Implementation, validation guards and interaction order
/**
     * Sets type of the given class for stricter checking.
     *
     *
     * @param string $type
     * @return self
     */
    public function setType(string $type): self
    {
        $this->type = $type;
        return $this;
    }

setConstructorArgs

Source line 72

public function setConstructorArgs(array $constructorArgs): self

Parameters

Parameter Type, modifiers and default Purpose
$constructorArgs array $constructorArgs * @return self

Sets constructor arguments which will be passed on object creation.

Array must be in format: ["arg1", "arg2"...]

@param array $constructorArgs @return self

State writes: $this->constructorArgs.

Return expressions (including local callbacks): $this.

Implementation, validation guards and interaction order
/**
     * Sets constructor arguments which will be passed on object creation.
     *
     * Array must be in format: ["arg1", "arg2"...]
     *
     *
     * @param array $constructorArgs
     * @return self
     */
    public function setConstructorArgs(array $constructorArgs): self
    {
        $this->constructorArgs = $constructorArgs;
        return $this;
    }

setProperties

Source line 88

public function setProperties(array $properties): self

Parameters

Parameter Type, modifiers and default Purpose
$properties array $properties * @return $this

Sets $this->properties from given array.

Array must be in format ["key" => "value"]. Where key is property name.

@param array $properties @return $this

State writes: $this->properties.

Return expressions (including local callbacks): $this.

Implementation, validation guards and interaction order
/**
     * Sets $this->properties from given array.
     *
     * Array must be in format ["key" => "value"].
     * Where key is property name.
     *
     * @param array $properties
     * @return $this
     */
    public function setProperties(array $properties): self
    {
        $this->properties = $properties;
        return $this;
    }

addProperty

Source line 102

public function addProperty(string $name, string $value): self

Parameters

Parameter Type, modifiers and default Purpose
$name string $name * @param string $value
$value string $value Operation input; the exact use, validation and propagation are shown in the implementation trace.

Dynamically adds property to the properties

@param string $name @param string $value @return $this

State writes: $this->properties[$name].

Return expressions (including local callbacks): $this.

Implementation, validation guards and interaction order
/**
     * Dynamically adds property to the properties
     *
     * @param string $name
     * @param string $value
     * @return $this
     */
    public function addProperty(string $name, string $value): self
    {
        $this->properties[$name] = $value;
        return $this;
    }

setMethodCalls

Source line 113

public function setMethodCalls(array $methodCalls): self

Parameters

Parameter Type, modifiers and default Purpose
$methodCalls array $methodCalls * @return self

Sets the method calls array. @param array $methodCalls @return self

State writes: $this->methodCalls.

Return expressions (including local callbacks): $this.

Implementation, validation guards and interaction order
/**
     * Sets the method calls array.
     * @param array $methodCalls
     * @return self
     */
    public function setMethodCalls(array $methodCalls): self
    {
        $this->methodCalls = $methodCalls;
        return $this;
    }

setMethodArgs

Source line 124

public function setMethodArgs(array $methodArgs): self

Parameters

Parameter Type, modifiers and default Purpose
$methodArgs array $methodArgs * @return self

Sets the method arguments. @param array $methodArgs @return self

State writes: $this->methodArgs.

Return expressions (including local callbacks): $this.

Implementation, validation guards and interaction order
/**
     * Sets the method arguments.
     * @param array $methodArgs
     * @return self
     */
    public function setMethodArgs(array $methodArgs): self
    {
        $this->methodArgs = $methodArgs;
        return $this;
    }

setProperty

Source line 137

protected function setProperty(object $object): object

Parameters

Parameter Type, modifiers and default Purpose
$object object $object * @return self

Sets values from $properties to the object. @param object $object @return self @throws \Exception if DEBUG = true, on non-existing properties in the class.

Return expressions (including local callbacks): $object.

Explicit exceptions: \Exception. The guards in the implementation below show when each is raised; exceptions from collaborators also propagate unless caught.

Implementation, validation guards and interaction order
/**
     * Sets values from $properties to the object.
     * @param object $object
     * @return self
     * @throws \Exception if DEBUG = true, on non-existing properties in the class.
     */
    protected function setProperty(object $object): object
    {
        if (count($this->properties) === 0)
            return $object;

        foreach ($this->properties as $property => $value) {
            if (property_exists($object, $property) || $object instanceof Model) {
                $object->{$property} = $value;
            } else {
                if (DEBUG)
                    throw new \Exception(get_class($object) . '::$' . $property . ' does not exist in ' . $this->classPath);
            }
        }

        return $object;
    }

addMethodCall

Source line 160

public function addMethodCall(string $name): self

Parameters

Parameter Type, modifiers and default Purpose
$name string $name The name of the method to add.

Adds a method call to the internal list. @param string $name The name of the method to add. @return self

State writes: $this->methodCalls[].

Return expressions (including local callbacks): $this.

Implementation, validation guards and interaction order
/**
     * Adds a method call to the internal list.
     * @param string $name The name of the method to add.
     * @return self
     */
    public function addMethodCall(string $name): self {
        $this->methodCalls[] = $name;
        return $this;
    }

addMethodArgs

Source line 171

public function addMethodArgs(string $method, array $args): self

Parameters

Parameter Type, modifiers and default Purpose
$method string $method The name of the method.
$args array $args The arguments to associate with the method.

Adds arguments for a specific method. @param string $method The name of the method. @param array $args The arguments to associate with the method. @return self

State writes: $this->methodArgs[$method].

Return expressions (including local callbacks): $this.

Implementation, validation guards and interaction order
/**
     * Adds arguments for a specific method.
     * @param string $method The name of the method.
     * @param array $args The arguments to associate with the method.
     * @return self
     */
    public function addMethodArgs(string $method, array $args): self {
        $this->methodArgs[$method] = $args;
        return $this;
    }

setClassName

Source line 181

public function setClassName(string $class): self

Parameters

Parameter Type, modifiers and default Purpose
$class string $class The name of the class to set.

Sets the class name for the current instance. @param string $class The name of the class to set. @return self

State writes: $this->className.

Return expressions (including local callbacks): $this.

Implementation, validation guards and interaction order
/**
     * Sets the class name for the current instance.
     * @param string $class The name of the class to set.
     * @return self
     */
    public function setClassName(string $class): self
    {
        $this->className = $class;
        return $this;
    }

checkType

Source line 193

public function checkType(object $object, string $type): bool

Parameters

Parameter Type, modifiers and default Purpose
$object object $object * @param string $type
$type string $type Expected class/type or enum selection according to the signature.

Checks if passed object matches the given type. @param object $object @param string $type @return bool

Return expressions (including local callbacks): true; false.

Implementation, validation guards and interaction order
/**
     * Checks if passed object matches the given type.
     * @param object $object
     * @param string $type
     * @return bool
     */
    public function checkType(object $object, string $type): bool
    {
        if ($object instanceof $type)
            return true;

        return false;
    }

callMethods

Source line 214

protected function callMethods(object $object): object

Parameters

Parameter Type, modifiers and default Purpose
$object object $object * @return self

Calls a method from methodCalls array. If same method exists in methodArgs args will be passed in order they were set.

Array: $methodCalls = ['method1', 'method2'] Array: $methodArgs = ['method1' => ['arg1', true, 'arg2']] Result: $object->{$methodCall}($arg1, $arg2, $arg3), $object->{$methodCall}()

@param object $object @return self @throws \Exception if DEBUG = true, on non-existing method call.

Return expressions (including local callbacks): $object.

Explicit exceptions: \Exception. The guards in the implementation below show when each is raised; exceptions from collaborators also propagate unless caught.

Implementation, validation guards and interaction order
/**
     *
     * Calls a method from `methodCalls` array.
     * If same method exists in `methodArgs` args will be passed in order they were set.
     *
     * Array: $methodCalls = ['method1', 'method2']
     * Array: $methodArgs = ['method1' => ['arg1', true, 'arg2']]
     * Result: $object->{$methodCall}($arg1, $arg2, $arg3), $object->{$methodCall}()
     *
     * @param object $object
     * @return self
     * @throws \Exception if DEBUG = true, on non-existing method call.
     */
    protected function callMethods(object $object): object
    {
        if (count($this->methodCalls) === 0)
            return $object;

        foreach ($this->methodCalls as $methodCall) {

            if (method_exists($object, $methodCall)) {

                if (isset($this->methodArgs[$methodCall])) {

                    $args = $this->methodArgs[$methodCall];

                    if (!is_array($args)) {
                        $args = [$args];
                    }

                    $object->{$methodCall}(...$args);

                } else {
                    $object->{$methodCall}();
                }

            } else {
                if (DEBUG)
                    throw new \Exception(get_class($object) . '::' . $methodCall . ' does not exist in ' . $this->classPath);
            }
        }

        return $object;
    }

create

Source line 254

public function create(): ?object

Creates and returns an instance of a class based on the provided class name, file path, or type. Attempts to dynamically load and instantiate a class from the specified path or directly by name. Throws exceptions or returns null in cases where class loading or instantiation fails, depending on the debug mode.

@return object|null The created object instance or null if instantiation fails.

Return expressions (including local callbacks): null; $object.

Explicit exceptions: \Exception. The guards in the implementation below show when each is raised; exceptions from collaborators also propagate unless caught.

Failure handling catches ReflectionException.

External effects include include_once.

Implementation, validation guards and interaction order
/**
     * Creates and returns an instance of a class based on the provided class name, file path, or type.
     * Attempts to dynamically load and instantiate a class from the specified path or directly by name.
     * Throws exceptions or returns null in cases where class loading or instantiation fails, depending on the debug mode.
     *
     * @return object|null The created object instance or null if instantiation fails.
     */
    public function create(): ?object
    {
        $classToInstantiate = null;

        if ($this->className !== null) {
            if ($this->classPath !== null && !class_exists($this->className)) {
                include_once $this->classPath;
            }
            $classToInstantiate = $this->className;
        } elseif ($this->classPath !== null) {
            $classToInstantiate = ObjectHandler::classFromFile($this->classPath);

        }

        if ($classToInstantiate === null) {
            if (DEBUG)
                throw new \Exception("Could not determine class to instantiate from the provided path or name.");
            return null;
        }

        try {
            $reflection = new ReflectionClass($classToInstantiate);
        } catch (ReflectionException $e) {
            if (DEBUG)
                throw new \Exception("Class '{$classToInstantiate}' not found or could not be reflected.", 0, $e);
            return null;
        }

        if ($reflection->isAbstract()) {
            if (DEBUG)
                throw new \Exception("Cannot create an instance of abstract class {$classToInstantiate}.");
            return null;
        }

        $object = (count($this->constructorArgs) > 0)
            ? $reflection->newInstanceArgs($this->constructorArgs)
            : $reflection->newInstance();

        if ($this->type !== null && !$this->checkType($object, $this->type)) {
            if (DEBUG) throw new \Exception("Object of class " . get_class($object) . " is not an instance of expected type {$this->type}");
            return null;
        }

        $object = $this->setProperty($object);
        $object = $this->callMethods($object);
        return $object;
    }

__destruct

Source line 302

public function __destruct()

State writes: $this->classPath, $this->className, $this->type, $this->constructorArgs, $this->methodCalls, $this->methodArgs, $this->properties.

Implementation, validation guards and interaction order
public function __destruct()
    {
        $this->classPath = null;
        $this->className = null;
        $this->type = null;
        $this->constructorArgs = [];
        $this->methodCalls = [];
        $this->methodArgs = [];
        $this->properties = [];
    }

Related types

Clone this wiki locally