Created
April 5, 2011 21:14
-
-
Save stefanooldeman/904567 to your computer and use it in GitHub Desktop.
Dependency Injection With default configuration demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* inspired on a series of articles from @fabpot | |
* url: http://fabien.potencier.org/article/11/what-is-dependency-injection | |
* | |
* These examples are made by @stefanooldeman and its purpose is to demo the idea and | |
* tooling of DI and make it clear in very short 'real world' example without pointing to any framework. | |
* btw: i did not see symfony's sfServiceContainer DI helpe module (yet) but i get the idea ;) | |
*/ | |
//todo: autoload or include ServiceContainer and User class | |
//This could be any class. but for now a simple one | |
class MyCacheHelper { | |
private $config; | |
public function __construct($config) { | |
$this->config = $config; | |
} | |
} | |
//configure the defeault dependency object | |
$service = new ServiceContainer(array('cache' => 'MyCacheHelper')); | |
$user = new User(); | |
var_dump($user->getCache()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class ServiceContainer implements ArrayAccess { | |
static protected $instance; | |
public $parameters = array(); | |
public function __construct(array $parameters = array()) { | |
$this->parameters = $parameters; | |
if(self::$instance == null) { | |
self::$instance = $this; | |
} | |
} | |
//implement interface | |
public function offsetSet($offset, $value) { | |
self::$instance->parameters[$offset] = $value; | |
} | |
public function offsetExists($offset) { | |
return isset(self::$instance->parameters[$offset]); | |
} | |
public function offsetUnset($offset) { | |
unset(self::$instance->parameters[$offset]); | |
} | |
public function offsetGet($offset) { | |
return isset(self::$instance->parameters[$offset]) ? self::$instance->parameters[$offset] : null; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class User extends ServiceContainer { | |
static protected $shared = null; | |
protected function getCacheConfig() { | |
return array( | |
'lifetime' => (7 * 24 * 60 * 60) | |
//whatever else you need | |
); | |
} | |
public function getCache() { | |
if(!isset(self::$shared)) { | |
$class = new $this['cache']($this->getCacheConfig()); | |
self::$shared = $class; | |
} | |
return self::$shared; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment