Hi,
you could simply write your own, you simply need a class or a function that
given an identifier such as "database.connector" would return you an
instance of it, and
and maybe handle singletons.
It just a question of taste of how you want it to be.
I have just posted a few days ago my implementation, it might interest you
(I think it should work with php 5.2)
_configuration = $configuration;
}
public function getConfiguration() {
return $this->_configuration;
}
public function __isset($serviceName) {
return isset($this->_serviceInstances[$serviceName]);
}
public function __call($serviceName, $args) {
// singleton
if (isset($this->_configuration[$serviceName]) &&
$this->_configuration[$serviceName]['singleton']) {
if (!isset($this->_singletonInstances[$serviceName])) {
$rc = new
ReflectionClass($this->_configuration[$serviceName]['class']);
$this->_singletonInstances[$serviceName] = empty($args) ?
$rc->newInstance() : $rc->newInstanceArgs($args);
}
$ret = $this->_singletonInstances[$serviceName];
} else {
// normal
if (isset($this->_setInstances[$serviceName])) {
$ret = $this->_setInstances[$serviceName];
unset($this->_setInstances[$serviceName]);
} else {
$rc = new
ReflectionClass($this->_configuration[$serviceName]['class']);
$ret = $this->_singletonInstances[$serviceName] =
empty($args) ? $rc->newInstance() : $rc->newInstanceArgs($args);
}
}
return $ret;
}
public function __get($serviceName) {
return $this->__call($serviceName, array());
}
public function __set($serviceName, $instance) {
if (!is_object($instance))
throw new Exception('instance must be an object');
$this->_setInstances[$serviceName] = $instance;
}
}
$di=Dependency::getInstance();
$di->setConfiguration(array(
'database.connector'=>array(
'singleton'=>true,
'class'=>'DatabaseConnector'
),
'database'=>array(
'singleton'=>true,
'class'=>'Database'
)
));
class DatabaseConnector{}
class Database{
protected $_connector;
public function __construct(){
$this->setConnector(Dependency::getInstance()->{'database.connector'});
}
public function setConnector($connector){
$this->_connector=$connector;
}
}
class Test{
protected $_database;
public function __construct(){
$this->setDatabase(Dependency::getInstance()->database);
}
public function setDatabase($db){
$this->_database=$db;
}
public function getDatabase(){
return $this->_database;
}
}
$test=new Test();
var_dump($test->getDatabase());
regards,
Jean-Baptiste Verrey
On 31 October 2011 21:32, robert mena wrote:
> Hi,
>
> I am trying to avoid reinventing the wheel so I am looking for
> dependency injection containers that work with PHP 5.2. So Symphony2
> and ZF2 DI are out of question.
>
> I found this
> http://www.potstuck.com/2010/09/09/php-dependency-a-php-dependency-injection-framework/
> but I was wondering if anyone has a opinion about it or alternatives.
>
> regards.
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>