I have a project that I worked on around a year ago. I've coded various things in it and it was at a time where I was just starting out on the road to learning dependency injection.
Long story short, I've forgotten how to pass a parameter to a constructor. I've tried, although it doesn't seem to do anything. I've tried looking at the existing code for Dice but I can't work it out.
<?php declare(strict_types = 1);
$dice = new \Dice\Dice;
$configRule = [
"constructParams" => ["config_directory_here"],
];
$dice->addRule("App\Providers\Configuration", $configRule);
The class in question
<?php declare(strict_types = 1);
namespace App\Providers;
class Configuration {
private $config;
public function __construct($configDirectory) {
exit($configDirectory);
$this->config = array();
$this->loadConfig($configDirectory);
}
}
As you can see, in the constructor I have this:
exit($configDirectory);
I don't get anything back, it just continues with the code and prints the Twig template, shouldn't it at least just print an empty string even if it didn't get passed?
I've not sure if this is a problem with my dice code or my PHP itself. I have PS4 autoloading so the Configuration class should be included.
"autoload": {
"psr-4": {
"App\\": "src"
}
}
Can anyone help me here? Thanks! When adding the below code it works as expected, but I need to use a Dependency Injection Container to load the class.
$config = new \App\Providers\Config("test");
echothenexitfor making sure? PHP surely passes constructor parameters. Seems you are asking the specific way of doing DI that is preconfigured in a certain framework.Configurationwith DIC.