I'm working on my HMVC project.
First: I have a list of helper/utilities classes, used to accomplish specific tasks: Arrays
, Classes
, Files
, Log
, Messages
, Images
, Encryption
, etc.
For example, in the Arrays
class I defined a function to read an array value in a multidimensional array by a given keypath (ex: get 'app/paths/modules' value).
For some classes, some of them are required dependencies, therefore beeing mandatory to be injected in constructors. Like, the Array
class is a mandatory dependency of the Config
class, used to read the values from the application configurations array.
For other classes they are not required dependencies. Like the Log
class. It's used sometimes in controller actions, or in QueryBuilder
methods, for logging purposes.
I would like to know, how the instances of this type of classes should be injected.
- As setter dependencies, or
- as arguments in the methods which are using them?
Second: Should I consider a Request
class as a required, e.g. constructor dependency in controllers?
I'm also using a dependency injection container.
Thank you.
Edit
Removed bullet lists from "For some classes..." and "For other classes...".
EDIT 2
Here is an example of using Arrays
class inside ConnectionConfiguration
:
/*
* Connection configuration.
*/
namespace [...]\Database;
use PDO;
use [...]\Config;
use [...]\Utils\Arrays;
/**
* Connection configuration.
*/
class ConnectionConfiguration {
/**
* Config.
*
* @var Config
*/
private $config;
/**
* Arrays.
*
* @var Arrays
*/
private $arrays;
/**
* Connection name.
*
* @var string
*/
private $connectionName;
/**
* Default connection attributes.
*
* @var array
*/
private $defaultConnectionAttributes = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_EMULATE_PREPARES => FALSE,
PDO::ATTR_PERSISTENT => TRUE
);
/**
*
* @param Config $config Config.
*/
public function __construct(Config $config, Arrays $arrays) {
$this
->setConfig($config)
->setArrays($arrays);
}
/**
* Get the connection attributes (merged over the default ones).
*
* @return array
*/
public function getConnectionAttributes() {
$defaultAttributes = $this->getDefaultConnectionAttributes();
$connectionAttributes = $this->getConnectionAttributes();
return $this->getArrays()->mergeArrays($defaultAttributes, $connectionAttributes);
}
}