22
ZEND FRAMEWORK 2 DEPENDENCY INJECTION h#p://slideshare.net/samsonasik

Zend Framework 2 : Dependency Injection

Embed Size (px)

DESCRIPTION

Zend Framework 2 : Dependency Injection

Citation preview

Page 1: Zend Framework 2 : Dependency Injection

ZEND  FRAMEWORK  2  DEPENDENCY  INJECTION  

   h#p://slideshare.net/samsonasik  

Page 2: Zend Framework 2 : Dependency Injection

Abdul  Malik  Ikhsan  

a.k.a  samsonasik  

~  Zend  Framework  specialist  

~  Codeigniter  Mentor  

On  twi#er  @samsonasik  Blog  h#p://samsonasik.wordpress.com  

Page 3: Zend Framework 2 : Dependency Injection

Apa  sih  “DI”  itu  ?  

Page 4: Zend Framework 2 : Dependency Injection

«  Dependency  InjecGon  is  where  components  are  given  their  dependencies  through  their  

constructors,  methods,  or  directly  into  fields.  »  

 h#p://www.picocontainer.org/injecGon.html    h#p://www.slideshare.net/fabpot/dependency-­‐injecGon  

Page 5: Zend Framework 2 : Dependency Injection

-­‐  DEPENDENCY  INJECTION  TERJADI  KETIKA  KOMPONEN  SOFTWARE  (  DALAM  HAL  INI,  KELAS  )  DEPENDENSINYA  DIBERIKAN  MELALUI  KONSTRUKTOR  MEREKA,  METHOD,  ATAU  LANGSUNG  KE  FIELDS  

Page 6: Zend Framework 2 : Dependency Injection

=>  PASSING  ATAU  SETTING  DEPENDENCY  KE  DALAM  KOMPONEN  SOFTWARE  

=>  JIKA  SEBUAH  KELAS  TIDAK  DAPAT  MELAKUKAN  PEKERJAANNYA  TANPA  DEPENDENCY,  MAKA  TERJADILAH  DEPENDENCY  INJECTION  

Page 7: Zend Framework 2 : Dependency Injection

-­‐  CONSTRUCTOR  INJECTION  -­‐  SETTER  INJECTION  -­‐  INTERFACE  INJECTION  

Page 8: Zend Framework 2 : Dependency Injection

CONSTRUCTOR  INJECTION  

class  Kalimat  {  

       protected  $filterstring;  

       public  funcGon  __construct(FilterString  $filterstring  )            {                  $this-­‐>filterstring  =  $filterstring  ;            }    }    

Page 9: Zend Framework 2 : Dependency Injection

SETTER  INJECTION  

class  Kalimat  {  

       protected  $filterstring;  

       public  funcGon  setFilterString(FilterString  $filterstring  )            {                  $this-­‐>filterstring  =  $filterstring  ;            }    }    

Page 10: Zend Framework 2 : Dependency Injection

INTERFACE  INJECTION  

interface  Kalimat  {  

       public  funcGon  setFilter(FilterString    $filterstring);  

}    

Page 11: Zend Framework 2 : Dependency Injection

2  KELAS  (  SAMPLE  )….  

Page 12: Zend Framework 2 : Dependency Injection

<?php  

class  FilterString  {          protected  $str;          public  funcGon  __construct($str)          {                  $this-­‐>str  =  ucfirst(  str_replace("  ","_",  $str)  );            }          public  funcGon  get()          {                  return  $this-­‐>str;            }  }  

Page 13: Zend Framework 2 : Dependency Injection

<?php  

class  Kalimat  {  

       protected  $filterstring;  

       public  funcGon  __construct(FilterString  $filterstring)  

       {  

               $this-­‐>filterstring  =  $filterstring;  

       }  

       public  funcGon  out()  

       {  

               echo  'ouGng  ....  -­‐>  ';  

               echo  $this-­‐>filterstring-­‐>get();          }  

}  

Page 14: Zend Framework 2 : Dependency Injection

TANPA  ZEND\DI  

<?php  

$filter  =  new  FilterString('saya  sedang  membaca');  

$kalimat  =  new  Kalimat($filter);  

$kalimat-­‐>out();  

Page 15: Zend Framework 2 : Dependency Injection

KITA  BUTUH  CONTAINER  !!!  -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐  /*  MENYIMPAN  OBJECT  DEFINITION  DAN  ENVIRONMENT,  MENG-­‐HANDLE  PENCIPTAAN  OBJECT  */  

Page 16: Zend Framework 2 : Dependency Injection

ZEND  FRAMEWORK  MEMPUNYAI  (  DEPENDENCY  INJECTION  COMPONENT  )  

Page 17: Zend Framework 2 : Dependency Injection

<?php  

$di  =  new  Zend\Di\Di;  $di-­‐>instanceManager()                -­‐>setParameters('FilterString',  array(                                'str'  =>  'saya  sedang  membaca’  ));  

$kalimat    =  $di-­‐>get('Kalimat');  //contains  FilterString  !  $kalimat-­‐>out();  

Page 18: Zend Framework 2 : Dependency Injection

MENGGUNAKAN  SETTER  ???  

Page 19: Zend Framework 2 : Dependency Injection

……………  public  funcGon  setFilterString(FilterString  $filterstring)  {  

       $this-­‐>filterstring  =  $filterstring;  }  

Page 20: Zend Framework 2 : Dependency Injection

$di  =  new  \Zend\Di\Di();  

$di-­‐>configure(new  \Zend\Di\ConfiguraGon(array(          'definiGon'  =>  array(  

               'class'  =>  array(                          'Kalimat'  =>  array(  

                               'setFilterString'  =>  array('required'  =>  true)  

                       )                  )  

       )  )));  

$kalimat  =  $di-­‐>get('Kalimat',    

 array(  'str'=>'saya  sedang  membaca'  )  );  

$kalimat-­‐>out();  

Page 21: Zend Framework 2 : Dependency Injection

TERIMA  KASIH  ;)  

Page 22: Zend Framework 2 : Dependency Injection

Referensi  :  •  h#p://www.picocontainer.org/injecGon.html    •  h#p://www.slideshare.net/fabpot/dependency-­‐injecGon  

•  h#p://mwop.net/slides/2011-­‐10-­‐18-­‐Zf2-­‐Overview/Zf2Overview.html#slide15  

•  h#p://akrabat.com/zend-­‐framework-­‐2/an-­‐introducGon-­‐to-­‐zenddi/  

Foto  :    •  h#p://www.as3dp.com/wp-­‐content/uploads/2010/10/dependencyInjecGon.png