MediaWiki REL1_30
|
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. | |
Construct objects from configuration instructions.
Definition at line 26 of file ObjectFactory.php.
|
static |
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 | $clazz | Class name |
array | $args | Constructor arguments |
Definition at line 129 of file ObjectFactory.php.
References $args.
|
staticprotected |
Iterate a list and call any closures it contains.
array | $list | List of things |
Definition at line 101 of file ObjectFactory.php.
References $value.
|
static |
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 | $spec | Object specification |
InvalidArgumentException | when object specification does not contain 'class' or 'factory' keys |
ReflectionException | when 'args' are supplied and 'class' constructor is non-public or non-existent |
Definition at line 58 of file ObjectFactory.php.
References $args.