SlideShare a Scribd company logo
ZEND	
  FRAMEWORK	
  2	
  
DEPENDENCY	
  INJECTION    	
  


     	
   	
  h#p://slideshare.net/samsonasik	
  
Abdul	
  Malik	
  Ikhsan	
  
a.k.a	
  samsonasik	
  

~	
  Zend	
  Framework	
  specialist	
  
~	
  Codeigniter	
  Mentor	
  
On	
  twi#er	
  @samsonasik	
  
Blog	
  h#p://samsonasik.wordpress.com	
  
Apa	
  sih	
  “DI”	
  itu	
  ?	
  
«	
  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	
  
-­‐	
  DEPENDENCY	
  INJECTION	
  TERJADI	
  
KETIKA	
  KOMPONEN	
  SOFTWARE	
  
(	
  DALAM	
  HAL	
  INI,	
  KELAS	
  )	
  
DEPENDENSINYA	
  DIBERIKAN	
  MELALUI	
  
KONSTRUKTOR	
  
MEREKA,	
  METHOD,	
  ATAU	
  LANGSUNG	
  
KE	
  FIELDS	
  
=>	
  PASSING	
  ATAU	
  SETTING	
  DEPENDENCY	
  
KE	
  DALAM	
  KOMPONEN	
  SOFTWARE	
  

=>	
  JIKA	
  SEBUAH	
  KELAS	
  TIDAK	
  DAPAT	
  
MELAKUKAN	
  PEKERJAANNYA	
  TANPA	
  
DEPENDENCY,	
  MAKA	
  TERJADILAH	
  
DEPENDENCY	
  INJECTION	
  
-­‐	
  CONSTRUCTOR	
  INJECTION	
  
-­‐	
  SETTER	
  INJECTION	
  
-­‐	
  INTERFACE	
  INJECTION	
  
CONSTRUCTOR	
  INJECTION	
  
class	
  Kalimat	
  
{	
  

	
  	
  	
  	
  protected	
  $filterstring;	
  

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

class	
  Kalimat	
  
{	
  

	
  	
  	
  	
  protected	
  $filterstring;	
  

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


interface	
  Kalimat	
  
{	
  
	
  	
  	
  	
  public	
  funcGon	
  setFilter(FilterString	
  	
  $filterstring);	
  
}	
  	
  
2	
  KELAS	
  (	
  SAMPLE	
  )….	
  
<?php	
  

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

class	
  Kalimat	
  {	
  
	
  	
  	
  	
  protected	
  $filterstring;	
  

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

	
  	
  	
  	
  public	
  funcGon	
  out()	
  
	
  	
  	
  	
  {	
  
	
  	
  	
  	
  	
  	
  	
  	
  echo	
  'ouGng	
  ....	
  -­‐>	
  ';	
  
	
  	
  	
  	
  	
  	
  	
  	
  echo	
  $this-­‐>filterstring-­‐>get();	
  
	
  	
  	
  	
  }	
  
}	
  
TANPA	
  ZENDDI	
  



<?php	
  

$filter	
  =	
  new	
  FilterString('saya	
  sedang	
  membaca');	
  
$kalimat	
  =	
  new	
  Kalimat($filter);	
  
$kalimat-­‐>out();	
  
KITA	
  BUTUH	
  CONTAINER	
  !!!	
  
-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐	
  
/*	
  
MENYIMPAN	
  OBJECT	
  DEFINITION	
  DAN	
  
ENVIRONMENT,	
  MENG-­‐HANDLE	
  
PENCIPTAAN	
  OBJECT	
  
*/	
  
ZEND	
  FRAMEWORK	
  MEMPUNYAI	
  
(	
  DEPENDENCY	
  INJECTION	
  
COMPONENT	
  )	
  
<?php	
  

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

$kalimat	
  	
  =	
  $di-­‐>get('Kalimat');	
  //contains	
  FilterString	
  !	
  
$kalimat-­‐>out();	
  
MENGGUNAKAN	
  SETTER	
  ???	
  
……………	
  
public	
  funcGon	
  setFilterString(FilterString	
  $filterstring)	
  
{	
  
	
  	
  	
  	
  $this-­‐>filterstring	
  =	
  $filterstring;	
  
}	
  
$di	
  =	
  new	
  ZendDiDi();	
  
$di-­‐>configure(new	
  ZendDiConfiguraGon(array(	
  
	
  	
  	
  	
  'definiGon'	
  =>	
  array(	
  
	
  	
  	
  	
  	
  	
  	
  	
  'class'	
  =>	
  array(	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  'Kalimat'	
  =>	
  array(	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  'setFilterString'	
  =>	
  array('required'	
  =>	
  true)	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  )	
  
	
  	
  	
  	
  	
  	
  	
  	
  )	
  
	
  	
  	
  	
  )	
  
)));	
  

$kalimat	
  =	
  $di-­‐>get('Kalimat',	
  	
  
   	
  array(	
  'str'=>'saya	
  sedang	
  membaca'	
  )	
  );	
  
$kalimat-­‐>out();	
  
TERIMA	
  KASIH	
  ;)	
  
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	
  

More Related Content

PDF
Business Rules with Brick
PPTX
Php & my sql
DOCX
Authentication Functions
PDF
14 Dependency Injection #burningkeyboards
PPTX
Refactoring group 1 - chapter 3,4,6
PPTX
PHP Traits
PPT
Php Chapter 1 Training
PDF
Web 4 | Core JavaScript
Business Rules with Brick
Php & my sql
Authentication Functions
14 Dependency Injection #burningkeyboards
Refactoring group 1 - chapter 3,4,6
PHP Traits
Php Chapter 1 Training
Web 4 | Core JavaScript

What's hot (20)

PDF
Web 8 | Introduction to PHP
PDF
Web 9 | OOP in PHP
PDF
Александр Трищенко: PHP 7 Evolution
PDF
PPT
Oracle PL sql 3
PDF
Benchmarking Perl Lightning Talk (NPW 2007)
PPT
Php variables
KEY
안드로이드 세미나 2
PPTX
Mysql:Operators
PDF
안드로이드 세미나 2
PPTX
PHP PPT FILE
PPT
Perl Intro 7 Subroutines
PDF
How to write code you won't hate tomorrow
PDF
Hacking parse.y (RubyKansai38)
PPT
Synapseindia php development tutorial
PDF
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
PDF
Data Types Master
PDF
What's New in Perl? v5.10 - v5.16
PDF
Sorting arrays in PHP
PPT
Javascript Experiment
Web 8 | Introduction to PHP
Web 9 | OOP in PHP
Александр Трищенко: PHP 7 Evolution
Oracle PL sql 3
Benchmarking Perl Lightning Talk (NPW 2007)
Php variables
안드로이드 세미나 2
Mysql:Operators
안드로이드 세미나 2
PHP PPT FILE
Perl Intro 7 Subroutines
How to write code you won't hate tomorrow
Hacking parse.y (RubyKansai38)
Synapseindia php development tutorial
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
Data Types Master
What's New in Perl? v5.10 - v5.16
Sorting arrays in PHP
Javascript Experiment
Ad

Viewers also liked (20)

PDF
Instant ACLs with Zend Framework 2
PDF
Software architectural pattern - MVC
PPTX
A SOA approximation on symfony
PDF
PHP is the King, nodejs the prince and python the fool
PDF
Zend Framework 2 - Basic Components
PDF
Asset management with Zend Framework 2
PPS
Implementing access control with zend framework
PPTX
Momchil Kyurkchiev Presentation
PPT
Zend Framework 2 - PHPUnit
PDF
Error Reporting in ZF2: form messages, custom error pages, logging
PDF
Codeigniter : Two Step View - Concept Implementation
PDF
2014 database - course 3 - PHP and MySQL
PPT
DB design
PDF
Unit testing PHP apps with PHPUnit
PDF
Into the ZF2 Service Manager
PPT
Codeigniter
PDF
CodeIgniter - PHP MVC Framework by silicongulf.com
PDF
Codeigniter : the security and the magic of hook
PPT
Week 3 database design
PPTX
Modular PHP Development using CodeIgniter Bonfire
Instant ACLs with Zend Framework 2
Software architectural pattern - MVC
A SOA approximation on symfony
PHP is the King, nodejs the prince and python the fool
Zend Framework 2 - Basic Components
Asset management with Zend Framework 2
Implementing access control with zend framework
Momchil Kyurkchiev Presentation
Zend Framework 2 - PHPUnit
Error Reporting in ZF2: form messages, custom error pages, logging
Codeigniter : Two Step View - Concept Implementation
2014 database - course 3 - PHP and MySQL
DB design
Unit testing PHP apps with PHPUnit
Into the ZF2 Service Manager
Codeigniter
CodeIgniter - PHP MVC Framework by silicongulf.com
Codeigniter : the security and the magic of hook
Week 3 database design
Modular PHP Development using CodeIgniter Bonfire
Ad

Similar to Zend Framework 2 : Dependency Injection (20)

PPT
ZFConf 2010: Zend Framework & MVC, Model Implementation (Part 2, Dependency I...
PDF
Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)
PDF
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)
PDF
Kicking off with Zend Expressive and Doctrine ORM (PHP Srbija 2017)
PDF
Migrare da symfony 1 a Symfony2
PPTX
Lecture 17 - PHP-Object-Orientation.pptx
PDF
Symfony2 from the Trenches
PDF
Design Patterns in PHP5
PDF
ZF2 for the ZF1 Developer
PDF
Symfony2 - from the trenches
PDF
OOP in PHP
PPTX
Creating "Secure" PHP Applications, Part 1, Explicit Code & QA
PPT
ZFConf 2010: Zend Framework & MVC, Model Implementation (Part 2, Dependency I...
PDF
Refactoring using Codeception
PDF
Beyond symfony 1.2 (Symfony Camp 2008)
PPTX
Coming to Terms with OOP In Drupal - php[world] 2016
PPTX
OOP Is More Then Cars and Dogs - Midwest PHP 2017
PPT
Php object orientation and classes
KEY
Symfony2 Building on Alpha / Beta technology
PDF
Kicking off with Zend Expressive and Doctrine ORM (ZendCon 2016)
ZFConf 2010: Zend Framework & MVC, Model Implementation (Part 2, Dependency I...
Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)
Kicking off with Zend Expressive and Doctrine ORM (PHP Srbija 2017)
Migrare da symfony 1 a Symfony2
Lecture 17 - PHP-Object-Orientation.pptx
Symfony2 from the Trenches
Design Patterns in PHP5
ZF2 for the ZF1 Developer
Symfony2 - from the trenches
OOP in PHP
Creating "Secure" PHP Applications, Part 1, Explicit Code & QA
ZFConf 2010: Zend Framework & MVC, Model Implementation (Part 2, Dependency I...
Refactoring using Codeception
Beyond symfony 1.2 (Symfony Camp 2008)
Coming to Terms with OOP In Drupal - php[world] 2016
OOP Is More Then Cars and Dogs - Midwest PHP 2017
Php object orientation and classes
Symfony2 Building on Alpha / Beta technology
Kicking off with Zend Expressive and Doctrine ORM (ZendCon 2016)

Recently uploaded (20)

PDF
Electronic commerce courselecture one. Pdf
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Encapsulation theory and applications.pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
Big Data Technologies - Introduction.pptx
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
KodekX | Application Modernization Development
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
Spectroscopy.pptx food analysis technology
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
Electronic commerce courselecture one. Pdf
sap open course for s4hana steps from ECC to s4
Encapsulation theory and applications.pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Big Data Technologies - Introduction.pptx
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
MIND Revenue Release Quarter 2 2025 Press Release
KodekX | Application Modernization Development
Chapter 3 Spatial Domain Image Processing.pdf
Encapsulation_ Review paper, used for researhc scholars
Spectroscopy.pptx food analysis technology
Reach Out and Touch Someone: Haptics and Empathic Computing
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
NewMind AI Weekly Chronicles - August'25 Week I
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Network Security Unit 5.pdf for BCA BBA.
20250228 LYD VKU AI Blended-Learning.pptx

Zend Framework 2 : Dependency Injection

  • 1. ZEND  FRAMEWORK  2   DEPENDENCY  INJECTION      h#p://slideshare.net/samsonasik  
  • 2. Abdul  Malik  Ikhsan   a.k.a  samsonasik   ~  Zend  Framework  specialist   ~  Codeigniter  Mentor   On  twi#er  @samsonasik   Blog  h#p://samsonasik.wordpress.com  
  • 3. Apa  sih  “DI”  itu  ?  
  • 4. «  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  
  • 5. -­‐  DEPENDENCY  INJECTION  TERJADI   KETIKA  KOMPONEN  SOFTWARE   (  DALAM  HAL  INI,  KELAS  )   DEPENDENSINYA  DIBERIKAN  MELALUI   KONSTRUKTOR   MEREKA,  METHOD,  ATAU  LANGSUNG   KE  FIELDS  
  • 6. =>  PASSING  ATAU  SETTING  DEPENDENCY   KE  DALAM  KOMPONEN  SOFTWARE   =>  JIKA  SEBUAH  KELAS  TIDAK  DAPAT   MELAKUKAN  PEKERJAANNYA  TANPA   DEPENDENCY,  MAKA  TERJADILAH   DEPENDENCY  INJECTION  
  • 7. -­‐  CONSTRUCTOR  INJECTION   -­‐  SETTER  INJECTION   -­‐  INTERFACE  INJECTION  
  • 8. CONSTRUCTOR  INJECTION   class  Kalimat   {          protected  $filterstring;          public  funcGon  __construct(FilterString  $filterstring  )            {                  $this-­‐>filterstring  =  $filterstring  ;            }     }    
  • 9. SETTER  INJECTION   class  Kalimat   {          protected  $filterstring;          public  funcGon  setFilterString(FilterString  $filterstring  )            {                  $this-­‐>filterstring  =  $filterstring  ;            }     }    
  • 10. INTERFACE  INJECTION   interface  Kalimat   {          public  funcGon  setFilter(FilterString    $filterstring);   }    
  • 11. 2  KELAS  (  SAMPLE  )….  
  • 12. <?php   class  FilterString   {          protected  $str;          public  funcGon  __construct($str)          {                  $this-­‐>str  =  ucfirst(  str_replace("  ","_",  $str)  );            }          public  funcGon  get()          {                  return  $this-­‐>str;            }   }  
  • 13. <?php   class  Kalimat  {          protected  $filterstring;          public  funcGon  __construct(FilterString  $filterstring)          {                  $this-­‐>filterstring  =  $filterstring;          }          public  funcGon  out()          {                  echo  'ouGng  ....  -­‐>  ';                  echo  $this-­‐>filterstring-­‐>get();          }   }  
  • 14. TANPA  ZENDDI   <?php   $filter  =  new  FilterString('saya  sedang  membaca');   $kalimat  =  new  Kalimat($filter);   $kalimat-­‐>out();  
  • 15. KITA  BUTUH  CONTAINER  !!!   -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐   /*   MENYIMPAN  OBJECT  DEFINITION  DAN   ENVIRONMENT,  MENG-­‐HANDLE   PENCIPTAAN  OBJECT   */  
  • 16. ZEND  FRAMEWORK  MEMPUNYAI   (  DEPENDENCY  INJECTION   COMPONENT  )  
  • 17. <?php   $di  =  new  ZendDiDi;   $di-­‐>instanceManager()                -­‐>setParameters('FilterString',  array(                                'str'  =>  'saya  sedang  membaca’   ));   $kalimat    =  $di-­‐>get('Kalimat');  //contains  FilterString  !   $kalimat-­‐>out();  
  • 19. ……………   public  funcGon  setFilterString(FilterString  $filterstring)   {          $this-­‐>filterstring  =  $filterstring;   }  
  • 20. $di  =  new  ZendDiDi();   $di-­‐>configure(new  ZendDiConfiguraGon(array(          'definiGon'  =>  array(                  'class'  =>  array(                          'Kalimat'  =>  array(                                  'setFilterString'  =>  array('required'  =>  true)                          )                  )          )   )));   $kalimat  =  $di-­‐>get('Kalimat',      array(  'str'=>'saya  sedang  membaca'  )  );   $kalimat-­‐>out();  
  • 22. 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