Skip to content

To support performance with PHP cache #4

@mostafabarmshory

Description

@mostafabarmshory

How it works

Pluf DI will read definitions from your configuration. When the container is compiled, PHP code will be generated based on those definitions.

For example let's take the definition for creating an object:

return [
    'Logger' => DI\create()
        ->constructor('/tmp/app.log')
        ->method('setLevel', 'warning'),
];

#[Component]
#[Qualifier("Logger")]
function createDbConnection(){
	$object = new Logger('/tmp/app.log');
	$object->setLevel('warning');
	return $object;
}

This definition will be compiled to PHP code similar to this:

$object = new Logger('/tmp/app.log');
$object->setLevel('warning');
return $object;

All the compiled definitions will be dumped into a PHP class (the compiled container) which will be written to a file (for example CompiledContainer.php).

At runtime, the container builder will see that the file CompiledContainer.php exists and will load it (instead of loading the definition files). That PHP file may contain a lot of code but PHP's opcode cache will cache that class in memory (remember to use opcache in production). When a definition needs to be resolved, PHP-DI will simply execute the compiled code and return the created instance.

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