I just started with this so don’t burn me :). I want to use PHP-DI with a PHP configuration format. But this class has a constructor (where you need to add values to the class).
For example. I have made a config.php file with this in it.
<?php
return [
'smush' => new \ReSmush\Helpers\Smushers\Resmush(),
];
Now I want to build the container using this
$containerBuilder = new \DI\ContainerBuilder();
$containerBuilder->addDefinitions('config.php');
$containerBuilder->build();
How can I can I call the container / class?
The class looks like this
class Resmush extends SmushApi
{
public function __construct($imageType, $imageFile)
{
$this->image->type = $imageType;
$this->image->file = $imageFile;
$this->url = 'http://api.resmush.it/?qlty=';
$this->exif = true;
}
}
I don’t think those parameters should be in the class constructor, since they’re not something the class in it’s entirety should know.
I would do it something like this:
class Resmush extends SmushApi
{
public function __construct()
{
}
public function reSmushImage($imageFile)
{
// imageType is not needed because there functions to get the image type from the file
}
}
And then you don’t need to pass any constructor parameters.
As for the container you should be able to do it like this:
non-lazy services will be created always in the container, regardless if you use them or not
Lazy services are only created if and when you use them (i.e. when you call $container->get('some_service'), otherwise (i.e. when your code never calls $container->get('some_service')) they won’t be created, saving CPU time and memory