MediaWiki REL1_28
ObjectFactory Class Reference

Construct objects from configuration instructions. More...

Static Public Member Functions

static constructClassInstance ( $clazz, $args)
 Construct an instance of the given class using the given arguments.
static getObjectFromSpec ( $spec)
 Instantiate an object based on a specification array.

Static Protected Member Functions

static expandClosures ( $list)
 Iterate a list and call any closures it contains.

Detailed Description

Construct objects from configuration instructions.

Bryan Davis bd808.nosp@m.@wik.nosp@m.imedi.nosp@m.a.or.nosp@m.g
© 2014 Bryan Davis and Wikimedia Foundation.

Definition at line 27 of file ObjectFactory.php.

Member Function Documentation

◆ constructClassInstance()

static ObjectFactory::constructClassInstance (   $clazz,

Construct an instance of the given class using the given arguments.

PHP's call_user_func_array() doesn't work with object construction so we have to use other measures. Starting with PHP 5.6.0 we could use the "splat" operator (...) to unpack the array into an argument list. Sadly there is no way to conditionally include a syntax construct like a new operator in a way that allows older versions of PHP to still parse the file. Instead, we will try a loop unrolling technique that works for 0-10 arguments. If we are passed 11 or more arguments we will take the performance penalty of using ReflectionClass::newInstanceArgs() to construct the desired object.

string$clazzClass name
array$argsConstructor arguments
mixed Constructed instance

Definition at line 130 of file ObjectFactory.php.

References $args.

◆ expandClosures()

static ObjectFactory::expandClosures (   $list)

Iterate a list and call any closures it contains.

array$listList of things
array List with any Closures replaced with their output

Definition at line 102 of file ObjectFactory.php.

References $value.

◆ getObjectFromSpec()

static ObjectFactory::getObjectFromSpec (   $spec)

Instantiate an object based on a specification array.

The specification array must contain a 'class' key with string value that specifies the class name to instantiate or a 'factory' key with a callable (is_callable() === true). It can optionally contain an 'args' key that provides arguments to pass to the constructor/callable.

Values in the arguments collection which are Closure instances will be expanded by invoking them with no arguments before passing the resulting value on to the constructor/callable. This can be used to pass IDatabase instances or other live objects to the constructor/callable. This behavior can be suppressed by adding closure_expansion => false to the specification.

The specification may also contain a 'calls' key that describes method calls to make on the newly created object before returning it. This pattern is often known as "setter injection". The value of this key is expected to be an associative array with method names as keys and argument lists as values. The argument list will be expanded (or not) in the same way as the 'args' key for the main object.

array$specObject specification
InvalidArgumentExceptionwhen object specification does not contain 'class' or 'factory' keys
ReflectionExceptionwhen 'args' are supplied and 'class' constructor is non-public or non-existent

Definition at line 59 of file ObjectFactory.php.

References $args, and as.

The documentation for this class was generated from the following file: