patternphpMinor
Dependency Injector Container
Viewed 0 times
containerdependencyinjector
Problem
I've been working on my own PHP framework for my projects. I just finished coding my DI container. There is always room for improvement. Any input be it good or bad on my code would be appreciated.
Also, would using exceptions instead of
Should I move the higher API functions (
```
id = strtolower($id);
}
public function __toString() {
return (string) $this->id;
}
}
class ServiceContainer
{
private $services;
private $lastRegistered;
/**
* Register a service with the DI container
*
* @param string $id Name of the service
*
* @param string $class Class to attach to service
*
* @return object $this
*/
public function register($id, $class) {
$this->addService($id, $class);
return $this;
}
/**
* Add a parameter to a service
*
* @param mixed $parameter Parameter to add
*
* @param string $id Name of the service
*
* @return object $this
*/
public function addParameter($parameter, $id = null) {
$id = isset($id) ? $id : $this->lastRegistered;
$this->addService($id, null, array($parameter));
return $this;
}
/**
* Check if a service is registered
*
* @param string $id Name of the service
*
* @return bool
*/
public function isRegistered($id) {
$id = strtolower($id);
return array_key_exists($id, $this->services);
}
/**
* Add a service to the DI container
*
* @param string $id Name of the service
*
* @param string $class Class to attach to service
*
* @param array $parameters Array of parameters
*/
public function addService($id, $class, array $parameters = array()) {
$id = strtolower($id);
// Initialize the parameter array
if (! isset($this->services[$id]['parameters'])) $this->services[$
Also, would using exceptions instead of
die() be a better approach?Should I move the higher API functions (
register() and addParameter()) to a separate class?```
id = strtolower($id);
}
public function __toString() {
return (string) $this->id;
}
}
class ServiceContainer
{
private $services;
private $lastRegistered;
/**
* Register a service with the DI container
*
* @param string $id Name of the service
*
* @param string $class Class to attach to service
*
* @return object $this
*/
public function register($id, $class) {
$this->addService($id, $class);
return $this;
}
/**
* Add a parameter to a service
*
* @param mixed $parameter Parameter to add
*
* @param string $id Name of the service
*
* @return object $this
*/
public function addParameter($parameter, $id = null) {
$id = isset($id) ? $id : $this->lastRegistered;
$this->addService($id, null, array($parameter));
return $this;
}
/**
* Check if a service is registered
*
* @param string $id Name of the service
*
* @return bool
*/
public function isRegistered($id) {
$id = strtolower($id);
return array_key_exists($id, $this->services);
}
/**
* Add a service to the DI container
*
* @param string $id Name of the service
*
* @param string $class Class to attach to service
*
* @param array $parameters Array of parameters
*/
public function addService($id, $class, array $parameters = array()) {
$id = strtolower($id);
// Initialize the parameter array
if (! isset($this->services[$id]['parameters'])) $this->services[$
Solution
Throwing Exceptions is better than die(), yes. However, you should consider throwing your own defined Exceptions instead of any built in php ones. For example, you could throw and InvalidService Exception from the getService method.
Using namespaces will help you avoid naming collisions. A class named "Reference" is bound to collide (perhaps you are already and left them out here for simplicity).
You could combine register and add Parameter such that register accepts a second argument: an array of parameters:
Using namespaces will help you avoid naming collisions. A class named "Reference" is bound to collide (perhaps you are already and left them out here for simplicity).
You could combine register and add Parameter such that register accepts a second argument: an array of parameters:
$svc->register('foo', array(5, 'bar'));Code Snippets
$svc->register('foo', array(5, 'bar'));Context
StackExchange Code Review Q#18191, answer score: 2
Revisions (0)
No revisions yet.