Skip to content

Factories ignore default parameters #526

@Khartir

Description

@Khartir

When using a factorie for a parameter the default values are ignored. Instead the container or the definition is injected.
Example:

$builder = new \DI\ContainerBuilder();
$builder->addDefinitions([
	'test' => \DI\factory('htmlentities')->parameter('string', '<something>'),
]);
$container = $builder->build();
echo $container->get('test');

This fails with the following exception:
DI\Definition\Exception\InvalidDefinition: Entry "test" cannot be resolved: Unable to invoke the callable because no value was given for parameter 3 ($encoding)
Parameter 2 is not mentioned because the container and the definition are injected as per this test:

class test {

	public function dummy($a = null, $b = null) {
		echo get_class($a) . PHP_EOL;
		echo get_class($b);
	}
}
$builder = new \DI\ContainerBuilder();
$builder->addDefinitions([
	'factory_test' => \DI\factory(['test', 'dummy']),
]);
$container = $builder->build();
$test = $container->get('factory_test');

This produces:
DI\Container DI\Definition\FactoryDefinition

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions