ارئه من تو کدرکانف3 که هدف اصلیم توضیح مفاهیم زیر است:
Inversion of control
Dependency injection
Constructor injection
Property injection
Setter injection
Interface/trait injection
Dependency inversion principle
Dependency injection container
Service locator
JPA as Tools for an Object Oriented Domain Modell using Hibernate as JPA Vendor. Presentation was held on customer site in Berlin and transmitted to the Lünen Office.
JPA as Tools for an Object Oriented Domain Modell using Hibernate as JPA Vendor. Presentation was held on customer site in Berlin and transmitted to the Lünen Office.
5. <?php
class example
{
public function create()
{
$db = new PDO($dsn , 'username', 'password');
$id = $db->insert(/* $data */);
$mailer=new mailer(/* $transport */);
$mailer->send();
//……
return $id;
}
}
6. global
db_setup.php
<?php
$db = new PDO($dsn , 'username', 'password');
<?php
class example
{
public function create()
{
global $db;
global $mailer;
$id = $db->insert(/* $data */);
$mailer->send();
//……
return $id;
}
}
7. global
db_setup.php
<?php
$db = new PDO($dsn , 'username', 'password');
<?php
class example
{
public function create()
{
global $db;
global $mailer;
$id = $db->insert(/* $data */);
$db = null;
$mailer->send();
//……
return $id;
}
}
20. <?php
Interface dbInterface{public function insert($data);}
Interface mailerInreface{public function send();}
class db implements dbInterface
{
public function insert($data){/* TODO */}
}
class mailer implements mailerInreface
{
public function send(){ mail(); }
}
class example
{
protected $db;
protected $mailer;
public function __construct($container)
{
$this->db = $container['db'];
$this->mailer = $container['mailer'];
}
public function create()
{
$id = $this->db->insert(/* $data */);
$this->mailer->send();
//…
return $id;
}
}
$container['db'] = new db;
$container['mailer'] = new mailer;
$example = new example($container);
21. <?php
Interface dbInterface{public function insert($data);}
Interface mailerInreface{public function send();}
class db implements dbInterface
{
public function insert($data){/* TODO */}
}
class mailer implements mailerInreface
{
public function send(){ mail(); }
}
class example
{
protected $db;
protected $mailer;
public function __construct($container)
{
$this->db = $container['db'];
$this->mailer = $container['mailer'];
}
public function create()
{
$id = $this->db->insert(/* $data */);
$this->mailer->send();
//…
return $id;
}
}
$container['db'] = new db;
$container['mailer'] = new mailer;
$example = new example($container);
Service Locator
24. https://leanpub.com/mlaphp
At no point will we be passing the container into any of the
objects that need dependencies. To do so would be using a
pattern called Service Locator.
ما اگرcontainerواقع در کنیم ارسال دارد نیاز ها وابستگی به که آبجکتی به رااز(ضد)الگویی
نام باservice locatorکردیم استفاده.
25. <?php
Interface dbInterface{public function insert($data);}
Interface mailerInreface{public function send();}
class db implements dbInterface
{
public function insert($data){/* TODO */}
}
class mailer implements mailerInreface
{
public function send(){ mail(); }
}
class example
{
protected $db;
protected $mailer;
public function __construct($container)
{
$this->db = $container['db'];
if(!$this->db instanceof dbInterface){
throw new Exception('some error');
}
$this->mailer = $container['mailer'];
}
public function create()
{
$id = $this->db->insert(/* $data */);
$this->mailer->send();
//…
return $id;
}
}
$container['db'] = new db;
$container['mailer'] = new mailer;
$example = new example($container);
28. PSR-11: Container Interface
<?php
namespace PsrContainer;
interface ContainerInterface
{
public function get($id);
public function has($id);
}
/**
* Base interface representing a generic exception in a container.
*/
interface ContainerExceptionInterface
{
}
/**
* No entry was found in the container.
*/
interface NotFoundExceptionInterface extends ContainerExceptionInterface
{
}
29. Auto-wiring and Reflection
ساختن(create)توسط ها وابستگی تزریق وcontainerاتوماتیک صورت به.
<?php
class example
{
public function __construct(db $db, mailer $mailer)
{}
}
class mailer{}
class db{}
$reflector = new ReflectionClass('example');
$parameters = $reflector->getMethod('__construct')->getParameters();
foreach ($parameters as $param) {
echo $param->getClass()->name . "n";
}
db
mailer