SlideShare a Scribd company logo
1 of 30
Download to read offline
‫از‬new class‫و‬Dependency injection
‫تا‬PSR-11‫و‬Auto-wiring
‫عربی‬ ‫میالد‬
9‫نویس‬ ‫برنامه‬ ‫سال‬PHP
‫دهنده‬ ‫توسعه‬BSS/CRM
milad.arabi@gmail.com
https://twitter.com/ooghry
https://www.linkedin.com/in/miladarabi/
PSR-7‫و‬middleware-‫ها‬‫معرفی‬ ‫و‬Zend Expressive
bit.ly/coderconfpsr7
<?php
class example
{
public function create()
{
$db = new PDO($dsn , 'username', 'password');
$id = $db->insert(/* $data */);
$mailer=new mailer(/* $transport */);
$mailer->send();
//……
return $id;
}
}
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;
}
}
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;
}
}
Inversion of control
‫در‬،‫افزار‬‫نرم‬ ‫مهندسی‬‫کنترل‬ ‫وارونگی‬‫اختصاصی‬ ‫بخشهای‬ ‫که‬ ‫کند‬‫می‬ ‫توصیف‬ ‫را‬ ‫طراحی‬ ‫از‬ ‫ای‬‫گونه‬‫نوشته‬
‫یک‬ ‫از‬ ‫را‬ ‫کنترل‬ ‫جریان‬ ‫ای‬‫رایانه‬ ‫برنامه‬ ‫یک‬ ‫شده‬‫کتابخانه‬‫دریافت‬ ‫عمومی‬ ‫بازاستفاده‬ ‫قابلیت‬ ‫با‬‫کند‬‫می‬.
https://fa.wikipedia.org/wiki/%D9%88%D8%A7%D8%B1%D9%88%D9%86%DA%AF%DB%8C_%DA%A9%D9%86%D8%AA%D8%B1%D9%84
‫آن‬ ‫به‬ ‫آبجکت‬ ‫یک‬ ‫های‬ ‫وابستگی‬ ‫وقتی‬‫شود‬ ‫داده‬‫خ‬ ‫های‬ ‫وابستگی‬ ‫ساختن‬ ‫مسئول‬ ‫آبجکت‬ ‫آن‬ ‫و‬،‫نباشد‬ ‫ود‬
‫طراحی‬ ‫اصل‬ ‫ما‬inversion of control‫کردیم‬ ‫اجرایی‬ ‫را‬.
Dependency injection
‫وابستگی‬ ِ‫تزریق‬(‫انگلیسی‬ ‫به‬:Dependency Injection (DI))‫در‬‫الگوی‬ ،‫شیءگرا‬ ‫نویسی‬‫برنامه‬
‫نیازمندی‬ ِ‫تحلیل‬ ‫از‬ ‫رفتار‬ ِ‫جداکردن‬ ِ‫اصلی‬ ٔ‫ه‬‫قاعد‬ ‫با‬ ‫طراحیست‬(‫انگلیسی‬ ‫به‬:Dependency
Resolution:)‫فنی‬‫افزاری‬‫نرم‬ ِ‫مستقل‬ ‫بسیار‬ ِ‫های‬‫مؤلفه‬ ِ‫کردن‬‫تجزیه‬ ‫برای‬(‫انگلیسی‬ ‫به‬:Software
Components.)‫به‬‫خا‬ ‫های‬‫وابستگی‬ ‫تزریق‬ ‫جهت‬ ‫است‬ ‫الگویی‬ ،‫وابستگی‬ ‫تزریق‬ ‫خالصه‬ ‫صورت‬‫رجی‬
‫کالس‬ ‫درون‬ ‫در‬ ‫ها‬‫وابستگی‬‫آن‬ ‫از‬ ‫مستقیم‬ ‫استفاده‬ ‫بجای‬ ،‫آن‬ ‫به‬ ‫کالس‬ ‫یک‬.
https://fa.wikipedia.org/wiki/%D8%AA%D8%B2%D8%B1%DB%8C%D9%82_%D9%86%DB%8C%D8%A7%D8%B2%D9%85%D9%86%D8%AF%DB%8C
dependency injection‫به‬‫چیز‬ ‫به‬ ‫آبجکتی‬ ‫اگر‬ ‫که‬ ‫است‬ ‫معنی‬ ‫این‬(‫یا‬ ‫آرایه‬ ‫یا‬ ‫آبجکت‬)...‫دیگری‬
‫آبجکت‬ ‫برای‬ ‫را‬ ‫آن‬ ‫نویس‬ ‫برنامه‬ ‫عنوان‬ ‫به‬ ‫ما‬ ،‫آبجکت‬ ‫داخل‬ ‫در‬ ‫آن‬ ‫ساخت‬ ‫جای‬ ‫به‬ ‫داشت‬ ‫احتیاج‬
‫ارسال‬(inject)‫کنیم‬.
Dependency injection
1. Constructor injection
2. Property injection
3. Setter injection
4. Interface injection
1.Constructor injection:
<?php
class example
{
protected $db;
private $mailer;
public function __construct(PDO $db, mailer $mailer)
{
$this->db = $db;
$this->mailer = $mailer;
}
public function create()
{
$id = $this->db->insert(/* $data */);
$this->mailer->send();
//…
return $id;
}
}
2.Property injection :
<?php
class example
{
public $db; //$example->db=new PDO();
public $mailer; //$example->mailer=new mailer;
public function create()
{
$id = $this->db->insert(/* $data */);
$this->mailer->send();
//…
return $id;
}
}
3.Setter injection:
<?php
class example
{
protected $db;
protected $mailer;
public function setDb(PDO $db)
{
$this->db = $db;
}
public function setMailer(mailer $mailer)
{
$this->mailer = $mailer;
}
public function create()
{
$id = $this->db->insert(/* $data */);
$this->mailer->send();
//…
return $id;
}
}
4.Interface injection
<?php
interface setDbInterface
{
public function setDb(PDO $db);
}
interface setMailerInterface
{
public function setMailer(mailer $mailer);
}
class example implements setDbInterface, setMailerInterface
{
protected $db;
protected $mailer;
public function setDb(PDO $db)
{
$this->db = $db;
}
public function setMailer(mailer $mailer)
{
$this->mailer = $mailer;
}
public function create()
{
$id = $this->db->insert(/* $data */);
$this->mailer->send();
//…
return $id;
}
}
4.Interface Trait injection
<?php
trait setDbTrait
{
public function setDb(PDO $db)
{
$this->db = $db;
}
}
trait setMailerTrait
{
public function setMailer(mailer $mailer)
{
$this->mailer = $mailer;
}
}
class example
{
use setDbTrait , setMailerTrait;
protected $db;
protected $mailer;
public function create()
{
$id = $this->db->insert(/* $data */);
$this->mailer->send();
//…
return $id;
}
}
SOLID - Dependency inversion principle
•‫انتزاعات‬ ‫به‬ ‫باید‬ ‫دو‬ ‫هر‬ ،‫باشند‬ ‫وابسته‬ ‫پایین‬ ‫سطح‬ ‫ماژولهای‬ ‫به‬ ‫نباید‬ ‫باال‬ ‫سطح‬ ‫های‬‫ماژول‬(abstract
‫و‬interface)‫باشند‬ ‫وابسته‬.
•‫باش‬ ‫انتزاعات‬ ‫به‬ ‫وابسته‬ ‫باید‬ ‫جزئیات‬ ‫بلکه‬ ،‫باشند‬ ‫جزئیات‬ ‫به‬ ‫وابسته‬ ‫نباید‬ ‫انتزاعات‬‫ند‬.
<?php
Interface dbInterface
{
public function insert($data);
}
Interface mailerInreface
{
public function send();
}
class example
{
protected $db;
protected $mailer;
public function __construct(dbInterface $db, mailerInreface $mailer)
{
$this->db = $db;
$this->mailer = $mailer;
}
public function create()
{
$id = $this->db->insert(/* $data */);
$this->mailer->send();
//…
return $id;
}
}
Dependency injection container
‫نگهداری‬ِ‫منطق‬ِ‫ایجاد‬‫ساخت‬ ‫اصلی‬ ‫ایده‬ ،‫نقطه‬ ‫یک‬ ‫در‬ ‫ها‬ ‫آبجکت‬dependency injection container-
‫باشد‬ ‫می‬ ‫ها‬.
container-‫به‬ ‫کانفیگ‬ ‫نوع‬ ‫اساس‬ ‫بر‬ ‫ها‬4‫شود‬ ‫می‬ ‫تقسیم‬ ‫دسته‬:
.1Pure php:pimple
.2XML‫و‬YAML:symfony
.3Annotation Base:Disco
.4Auto-wiring:symfony PHP-DI Illuminate
<?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);
<?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
<?php
interface MyServiceLocator
{
public function get($id);
public function has($id);
}
interface MyDependencyContianer
{
public function get($id);
public function has($id);
}
<?php
interface MyServiceLocator
{
public function get($id);
public function has($id);
}
interface MyDependencyContianer
{
public function get($id);
public function has($id);
}
Push vs Pull
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‫کردیم‬ ‫استفاده‬.
<?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);
instanceof
<?php
interface interface1{}
interface interface2{}
abstract class abstractClass{}
class myClass extends abstractClass implements interface1,interface2{}
class_alias('myClass','anotherClass');
$var=new anotherclass;
var_dump($var instanceof interface1); //true
var_dump($var instanceof interface2); //true
var_dump($var instanceof abstractClass); //true
var_dump($var instanceof myClass); //true
var_dump($var instanceof anotherClass); //true
Inversion of control:‫آن‬ ‫به‬ ‫آبجکت‬ ‫یک‬ ‫های‬ ‫وابستگی‬ ‫وقتی‬‫شود‬ ‫داده‬‫وابست‬ ‫ساختن‬ ‫مسئول‬ ‫آبجکت‬ ‫آن‬ ‫و‬‫گی‬
‫خود‬ ‫های‬‫نباشد‬(.design principle)
Dependency injection:‫اصل‬ ‫سازی‬ ‫پیاده‬ ‫برای‬ ‫هایی‬ ‫تکنیکی‬IoC
.1Constructor injection
.2Property injection
.3Setter injection
.4Interface injection
Dependency inversion Principle:‫انتزاعات‬ ‫بر‬ ‫تکیه‬ ‫برای‬ ‫اصلی‬(abstract , interface)
Dependency injection Container:‫باال‬ ‫موارد‬ ‫سازی‬ ‫پیاده‬ ‫برای‬ ‫ابزاری‬.
Service Locator:‫ارسال‬container‫کند‬ ‫می‬ ‫استفاده‬ ‫ها‬ ‫وابستگی‬ ‫آن‬ ‫از‬ ‫که‬ ‫کالسی‬ ‫به‬.
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
{
}
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
‫عربی‬ ‫میالد‬
milad.arabi@gmail.com
https://twitter.com/ooghry
https://www.linkedin.com/in/miladarabi/
‫متشکرم‬
‫از‬new class‫و‬Dependency injection
‫تا‬PSR-11‫و‬Auto-wiring

More Related Content

What's hot (14)

Testování prakticky
Testování praktickyTestování prakticky
Testování prakticky
 
Iteratory
IteratoryIteratory
Iteratory
 
es6.concurrency()
es6.concurrency()es6.concurrency()
es6.concurrency()
 
KvZ Web Tasarım Hizmetleri
KvZ Web Tasarım HizmetleriKvZ Web Tasarım Hizmetleri
KvZ Web Tasarım Hizmetleri
 
Proxy & CGLIB
Proxy & CGLIBProxy & CGLIB
Proxy & CGLIB
 
DeSymfony 2012: Symfony internals
DeSymfony 2012: Symfony internalsDeSymfony 2012: Symfony internals
DeSymfony 2012: Symfony internals
 
Curso Symfony - Clase 1
Curso Symfony - Clase 1Curso Symfony - Clase 1
Curso Symfony - Clase 1
 
Lab 10 rus razvan
Lab 10   rus razvanLab 10   rus razvan
Lab 10 rus razvan
 
Sis quiz
Sis quizSis quiz
Sis quiz
 
Wek14 mysql 2
Wek14 mysql 2Wek14 mysql 2
Wek14 mysql 2
 
JPA - Java Persistence API
JPA - Java Persistence APIJPA - Java Persistence API
JPA - Java Persistence API
 
Oop koncepti
Oop konceptiOop koncepti
Oop koncepti
 
Blogger template-squeeze-page-angelogrande
Blogger template-squeeze-page-angelograndeBlogger template-squeeze-page-angelogrande
Blogger template-squeeze-page-angelogrande
 
Dwr实战
Dwr实战Dwr实战
Dwr实战
 

from new class and dependency injection to PSR-11 and Auto-wiring

  • 2. ‫عربی‬ ‫میالد‬ 9‫نویس‬ ‫برنامه‬ ‫سال‬PHP ‫دهنده‬ ‫توسعه‬BSS/CRM milad.arabi@gmail.com https://twitter.com/ooghry https://www.linkedin.com/in/miladarabi/
  • 4.
  • 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; } }
  • 8. Inversion of control ‫در‬،‫افزار‬‫نرم‬ ‫مهندسی‬‫کنترل‬ ‫وارونگی‬‫اختصاصی‬ ‫بخشهای‬ ‫که‬ ‫کند‬‫می‬ ‫توصیف‬ ‫را‬ ‫طراحی‬ ‫از‬ ‫ای‬‫گونه‬‫نوشته‬ ‫یک‬ ‫از‬ ‫را‬ ‫کنترل‬ ‫جریان‬ ‫ای‬‫رایانه‬ ‫برنامه‬ ‫یک‬ ‫شده‬‫کتابخانه‬‫دریافت‬ ‫عمومی‬ ‫بازاستفاده‬ ‫قابلیت‬ ‫با‬‫کند‬‫می‬. https://fa.wikipedia.org/wiki/%D9%88%D8%A7%D8%B1%D9%88%D9%86%DA%AF%DB%8C_%DA%A9%D9%86%D8%AA%D8%B1%D9%84 ‫آن‬ ‫به‬ ‫آبجکت‬ ‫یک‬ ‫های‬ ‫وابستگی‬ ‫وقتی‬‫شود‬ ‫داده‬‫خ‬ ‫های‬ ‫وابستگی‬ ‫ساختن‬ ‫مسئول‬ ‫آبجکت‬ ‫آن‬ ‫و‬،‫نباشد‬ ‫ود‬ ‫طراحی‬ ‫اصل‬ ‫ما‬inversion of control‫کردیم‬ ‫اجرایی‬ ‫را‬.
  • 9. Dependency injection ‫وابستگی‬ ِ‫تزریق‬(‫انگلیسی‬ ‫به‬:Dependency Injection (DI))‫در‬‫الگوی‬ ،‫شیءگرا‬ ‫نویسی‬‫برنامه‬ ‫نیازمندی‬ ِ‫تحلیل‬ ‫از‬ ‫رفتار‬ ِ‫جداکردن‬ ِ‫اصلی‬ ٔ‫ه‬‫قاعد‬ ‫با‬ ‫طراحیست‬(‫انگلیسی‬ ‫به‬:Dependency Resolution:)‫فنی‬‫افزاری‬‫نرم‬ ِ‫مستقل‬ ‫بسیار‬ ِ‫های‬‫مؤلفه‬ ِ‫کردن‬‫تجزیه‬ ‫برای‬(‫انگلیسی‬ ‫به‬:Software Components.)‫به‬‫خا‬ ‫های‬‫وابستگی‬ ‫تزریق‬ ‫جهت‬ ‫است‬ ‫الگویی‬ ،‫وابستگی‬ ‫تزریق‬ ‫خالصه‬ ‫صورت‬‫رجی‬ ‫کالس‬ ‫درون‬ ‫در‬ ‫ها‬‫وابستگی‬‫آن‬ ‫از‬ ‫مستقیم‬ ‫استفاده‬ ‫بجای‬ ،‫آن‬ ‫به‬ ‫کالس‬ ‫یک‬. https://fa.wikipedia.org/wiki/%D8%AA%D8%B2%D8%B1%DB%8C%D9%82_%D9%86%DB%8C%D8%A7%D8%B2%D9%85%D9%86%D8%AF%DB%8C dependency injection‫به‬‫چیز‬ ‫به‬ ‫آبجکتی‬ ‫اگر‬ ‫که‬ ‫است‬ ‫معنی‬ ‫این‬(‫یا‬ ‫آرایه‬ ‫یا‬ ‫آبجکت‬)...‫دیگری‬ ‫آبجکت‬ ‫برای‬ ‫را‬ ‫آن‬ ‫نویس‬ ‫برنامه‬ ‫عنوان‬ ‫به‬ ‫ما‬ ،‫آبجکت‬ ‫داخل‬ ‫در‬ ‫آن‬ ‫ساخت‬ ‫جای‬ ‫به‬ ‫داشت‬ ‫احتیاج‬ ‫ارسال‬(inject)‫کنیم‬.
  • 10. Dependency injection 1. Constructor injection 2. Property injection 3. Setter injection 4. Interface injection
  • 11. 1.Constructor injection: <?php class example { protected $db; private $mailer; public function __construct(PDO $db, mailer $mailer) { $this->db = $db; $this->mailer = $mailer; } public function create() { $id = $this->db->insert(/* $data */); $this->mailer->send(); //… return $id; } }
  • 12. 2.Property injection : <?php class example { public $db; //$example->db=new PDO(); public $mailer; //$example->mailer=new mailer; public function create() { $id = $this->db->insert(/* $data */); $this->mailer->send(); //… return $id; } }
  • 13. 3.Setter injection: <?php class example { protected $db; protected $mailer; public function setDb(PDO $db) { $this->db = $db; } public function setMailer(mailer $mailer) { $this->mailer = $mailer; } public function create() { $id = $this->db->insert(/* $data */); $this->mailer->send(); //… return $id; } }
  • 14. 4.Interface injection <?php interface setDbInterface { public function setDb(PDO $db); } interface setMailerInterface { public function setMailer(mailer $mailer); } class example implements setDbInterface, setMailerInterface { protected $db; protected $mailer; public function setDb(PDO $db) { $this->db = $db; } public function setMailer(mailer $mailer) { $this->mailer = $mailer; } public function create() { $id = $this->db->insert(/* $data */); $this->mailer->send(); //… return $id; } }
  • 15. 4.Interface Trait injection <?php trait setDbTrait { public function setDb(PDO $db) { $this->db = $db; } } trait setMailerTrait { public function setMailer(mailer $mailer) { $this->mailer = $mailer; } } class example { use setDbTrait , setMailerTrait; protected $db; protected $mailer; public function create() { $id = $this->db->insert(/* $data */); $this->mailer->send(); //… return $id; } }
  • 16. SOLID - Dependency inversion principle •‫انتزاعات‬ ‫به‬ ‫باید‬ ‫دو‬ ‫هر‬ ،‫باشند‬ ‫وابسته‬ ‫پایین‬ ‫سطح‬ ‫ماژولهای‬ ‫به‬ ‫نباید‬ ‫باال‬ ‫سطح‬ ‫های‬‫ماژول‬(abstract ‫و‬interface)‫باشند‬ ‫وابسته‬. •‫باش‬ ‫انتزاعات‬ ‫به‬ ‫وابسته‬ ‫باید‬ ‫جزئیات‬ ‫بلکه‬ ،‫باشند‬ ‫جزئیات‬ ‫به‬ ‫وابسته‬ ‫نباید‬ ‫انتزاعات‬‫ند‬.
  • 17. <?php Interface dbInterface { public function insert($data); } Interface mailerInreface { public function send(); } class example { protected $db; protected $mailer; public function __construct(dbInterface $db, mailerInreface $mailer) { $this->db = $db; $this->mailer = $mailer; } public function create() { $id = $this->db->insert(/* $data */); $this->mailer->send(); //… return $id; } }
  • 18. Dependency injection container ‫نگهداری‬ِ‫منطق‬ِ‫ایجاد‬‫ساخت‬ ‫اصلی‬ ‫ایده‬ ،‫نقطه‬ ‫یک‬ ‫در‬ ‫ها‬ ‫آبجکت‬dependency injection container- ‫باشد‬ ‫می‬ ‫ها‬.
  • 19. container-‫به‬ ‫کانفیگ‬ ‫نوع‬ ‫اساس‬ ‫بر‬ ‫ها‬4‫شود‬ ‫می‬ ‫تقسیم‬ ‫دسته‬: .1Pure php:pimple .2XML‫و‬YAML:symfony .3Annotation Base:Disco .4Auto-wiring:symfony PHP-DI Illuminate
  • 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
  • 22. <?php interface MyServiceLocator { public function get($id); public function has($id); } interface MyDependencyContianer { public function get($id); public function has($id); }
  • 23. <?php interface MyServiceLocator { public function get($id); public function has($id); } interface MyDependencyContianer { public function get($id); public function has($id); } Push vs Pull
  • 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);
  • 26. instanceof <?php interface interface1{} interface interface2{} abstract class abstractClass{} class myClass extends abstractClass implements interface1,interface2{} class_alias('myClass','anotherClass'); $var=new anotherclass; var_dump($var instanceof interface1); //true var_dump($var instanceof interface2); //true var_dump($var instanceof abstractClass); //true var_dump($var instanceof myClass); //true var_dump($var instanceof anotherClass); //true
  • 27. Inversion of control:‫آن‬ ‫به‬ ‫آبجکت‬ ‫یک‬ ‫های‬ ‫وابستگی‬ ‫وقتی‬‫شود‬ ‫داده‬‫وابست‬ ‫ساختن‬ ‫مسئول‬ ‫آبجکت‬ ‫آن‬ ‫و‬‫گی‬ ‫خود‬ ‫های‬‫نباشد‬(.design principle) Dependency injection:‫اصل‬ ‫سازی‬ ‫پیاده‬ ‫برای‬ ‫هایی‬ ‫تکنیکی‬IoC .1Constructor injection .2Property injection .3Setter injection .4Interface injection Dependency inversion Principle:‫انتزاعات‬ ‫بر‬ ‫تکیه‬ ‫برای‬ ‫اصلی‬(abstract , interface) Dependency injection Container:‫باال‬ ‫موارد‬ ‫سازی‬ ‫پیاده‬ ‫برای‬ ‫ابزاری‬. Service Locator:‫ارسال‬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