User Tools

Site Tools


technical_notes:public:php-dependency-injection

Parent

PHP - Dependency Injection

class Mailer {
    private $transport;
    public function __construct($transport){
        $this->transport = $transport;
    }
}
 
class Smtp{
    public function __construct($host, $port, $user, $pass){
        // Process arguments
    }
}
 
$DiContainer = new DiContainer();
 
$DiContainer->register('smtp', 'Smtp')
    ->addArgument('host')
    ->addArgument('port')
    ->addArgument('user')
    ->addArgument('pass');
 
$DiContainer->register('mailer', 'Mailer')
    ->addArgument('@@smtp');
 
$mailer = $DiContainer->getInstance('mailer');

Resources

technical_notes/public/php-dependency-injection.txt · Last modified: 2014/04/14 10:48 by Fabien Arcellier