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	
  
Ad

Recommended

Business Rules with Brick
Business Rules with Brick
brian d foy
 
Php & my sql
Php & my sql
Norhisyam Dasuki
 
Authentication Functions
Authentication Functions
Valerie Rickert
 
14 Dependency Injection #burningkeyboards
14 Dependency Injection #burningkeyboards
Denis Ristic
 
Refactoring group 1 - chapter 3,4,6
Refactoring group 1 - chapter 3,4,6
Duy Lâm
 
PHP Traits
PHP Traits
mattbuzz
 
Php Chapter 1 Training
Php Chapter 1 Training
Chris Chubb
 
Web 4 | Core JavaScript
Web 4 | Core JavaScript
Mohammad Imam Hossain
 
Web 8 | Introduction to PHP
Web 8 | Introduction to PHP
Mohammad Imam Hossain
 
Web 9 | OOP in PHP
Web 9 | OOP in PHP
Mohammad Imam Hossain
 
Александр Трищенко: PHP 7 Evolution
Александр Трищенко: PHP 7 Evolution
Oleg Poludnenko
 
Php Enums
Php Enums
Ayesh Karunaratne
 
Oracle PL sql 3
Oracle PL sql 3
Sergio Ronchi
 
Benchmarking Perl Lightning Talk (NPW 2007)
Benchmarking Perl Lightning Talk (NPW 2007)
brian d foy
 
Php variables
Php variables
Ritwik Das
 
안드로이드 세미나 2
안드로이드 세미나 2
ang0123dev
 
Mysql:Operators
Mysql:Operators
DataminingTools Inc
 
안드로이드 세미나 2
안드로이드 세미나 2
Chul Ju Hong
 
PHP PPT FILE
PHP PPT FILE
AbhishekSharma2958
 
Perl Intro 7 Subroutines
Perl Intro 7 Subroutines
Shaun Griffith
 
How to write code you won't hate tomorrow
How to write code you won't hate tomorrow
Pete McFarlane
 
Hacking parse.y (RubyKansai38)
Hacking parse.y (RubyKansai38)
ujihisa
 
Synapseindia php development tutorial
Synapseindia php development tutorial
Synapseindiappsdevelopment
 
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
ssuserd6b1fd
 
Data Types Master
Data Types Master
Paolo Marcatili
 
What's New in Perl? v5.10 - v5.16
What's New in Perl? v5.10 - v5.16
Ricardo Signes
 
Sorting arrays in PHP
Sorting arrays in PHP
Vineet Kumar Saini
 
Javascript Experiment
Javascript Experiment
wgamboa
 
Instant ACLs with Zend Framework 2
Instant ACLs with Zend Framework 2
Stefano Valle
 
Software architectural pattern - MVC
Software architectural pattern - MVC
Abdul Malik Ikhsan
 

More Related Content

What's hot (20)

Web 8 | Introduction to PHP
Web 8 | Introduction to PHP
Mohammad Imam Hossain
 
Web 9 | OOP in PHP
Web 9 | OOP in PHP
Mohammad Imam Hossain
 
Александр Трищенко: PHP 7 Evolution
Александр Трищенко: PHP 7 Evolution
Oleg Poludnenko
 
Php Enums
Php Enums
Ayesh Karunaratne
 
Oracle PL sql 3
Oracle PL sql 3
Sergio Ronchi
 
Benchmarking Perl Lightning Talk (NPW 2007)
Benchmarking Perl Lightning Talk (NPW 2007)
brian d foy
 
Php variables
Php variables
Ritwik Das
 
안드로이드 세미나 2
안드로이드 세미나 2
ang0123dev
 
Mysql:Operators
Mysql:Operators
DataminingTools Inc
 
안드로이드 세미나 2
안드로이드 세미나 2
Chul Ju Hong
 
PHP PPT FILE
PHP PPT FILE
AbhishekSharma2958
 
Perl Intro 7 Subroutines
Perl Intro 7 Subroutines
Shaun Griffith
 
How to write code you won't hate tomorrow
How to write code you won't hate tomorrow
Pete McFarlane
 
Hacking parse.y (RubyKansai38)
Hacking parse.y (RubyKansai38)
ujihisa
 
Synapseindia php development tutorial
Synapseindia php development tutorial
Synapseindiappsdevelopment
 
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
ssuserd6b1fd
 
Data Types Master
Data Types Master
Paolo Marcatili
 
What's New in Perl? v5.10 - v5.16
What's New in Perl? v5.10 - v5.16
Ricardo Signes
 
Sorting arrays in PHP
Sorting arrays in PHP
Vineet Kumar Saini
 
Javascript Experiment
Javascript Experiment
wgamboa
 
Александр Трищенко: PHP 7 Evolution
Александр Трищенко: PHP 7 Evolution
Oleg Poludnenko
 
Benchmarking Perl Lightning Talk (NPW 2007)
Benchmarking Perl Lightning Talk (NPW 2007)
brian d foy
 
안드로이드 세미나 2
안드로이드 세미나 2
ang0123dev
 
안드로이드 세미나 2
안드로이드 세미나 2
Chul Ju Hong
 
Perl Intro 7 Subroutines
Perl Intro 7 Subroutines
Shaun Griffith
 
How to write code you won't hate tomorrow
How to write code you won't hate tomorrow
Pete McFarlane
 
Hacking parse.y (RubyKansai38)
Hacking parse.y (RubyKansai38)
ujihisa
 
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
ssuserd6b1fd
 
What's New in Perl? v5.10 - v5.16
What's New in Perl? v5.10 - v5.16
Ricardo Signes
 
Javascript Experiment
Javascript Experiment
wgamboa
 

Viewers also liked (20)

Instant ACLs with Zend Framework 2
Instant ACLs with Zend Framework 2
Stefano Valle
 
Software architectural pattern - MVC
Software architectural pattern - MVC
Abdul Malik Ikhsan
 
A SOA approximation on symfony
A SOA approximation on symfony
Joseluis Laso
 
PHP is the King, nodejs the prince and python the fool
PHP is the King, nodejs the prince and python the fool
Alessandro Cinelli (cirpo)
 
Zend Framework 2 - Basic Components
Zend Framework 2 - Basic Components
Mateusz Tymek
 
Asset management with Zend Framework 2
Asset management with Zend Framework 2
Stefano Valle
 
Implementing access control with zend framework
Implementing access control with zend framework
George Mihailov
 
Momchil Kyurkchiev Presentation
Momchil Kyurkchiev Presentation
Start It Smart
 
Zend Framework 2 - PHPUnit
Zend Framework 2 - PHPUnit
Tarun Kumar Singhal
 
Error Reporting in ZF2: form messages, custom error pages, logging
Error Reporting in ZF2: form messages, custom error pages, logging
Steve Maraspin
 
Codeigniter : Two Step View - Concept Implementation
Codeigniter : Two Step View - Concept Implementation
Abdul Malik Ikhsan
 
2014 database - course 3 - PHP and MySQL
2014 database - course 3 - PHP and MySQL
Hung-yu Lin
 
DB design
DB design
fikirabc
 
Unit testing PHP apps with PHPUnit
Unit testing PHP apps with PHPUnit
Michelangelo van Dam
 
Into the ZF2 Service Manager
Into the ZF2 Service Manager
Chris Tankersley
 
Codeigniter
Codeigniter
shadowk
 
CodeIgniter - PHP MVC Framework by silicongulf.com
CodeIgniter - PHP MVC Framework by silicongulf.com
Christopher Cubos
 
Codeigniter : the security and the magic of hook
Codeigniter : the security and the magic of hook
Abdul Malik Ikhsan
 
Week 3 database design
Week 3 database design
Fareez Borhanudin
 
Modular PHP Development using CodeIgniter Bonfire
Modular PHP Development using CodeIgniter Bonfire
Jeff Fox
 
Instant ACLs with Zend Framework 2
Instant ACLs with Zend Framework 2
Stefano Valle
 
Software architectural pattern - MVC
Software architectural pattern - MVC
Abdul Malik Ikhsan
 
A SOA approximation on symfony
A SOA approximation on symfony
Joseluis Laso
 
PHP is the King, nodejs the prince and python the fool
PHP is the King, nodejs the prince and python the fool
Alessandro Cinelli (cirpo)
 
Zend Framework 2 - Basic Components
Zend Framework 2 - Basic Components
Mateusz Tymek
 
Asset management with Zend Framework 2
Asset management with Zend Framework 2
Stefano Valle
 
Implementing access control with zend framework
Implementing access control with zend framework
George Mihailov
 
Momchil Kyurkchiev Presentation
Momchil Kyurkchiev Presentation
Start It Smart
 
Error Reporting in ZF2: form messages, custom error pages, logging
Error Reporting in ZF2: form messages, custom error pages, logging
Steve Maraspin
 
Codeigniter : Two Step View - Concept Implementation
Codeigniter : Two Step View - Concept Implementation
Abdul Malik Ikhsan
 
2014 database - course 3 - PHP and MySQL
2014 database - course 3 - PHP and MySQL
Hung-yu Lin
 
Unit testing PHP apps with PHPUnit
Unit testing PHP apps with PHPUnit
Michelangelo van Dam
 
Into the ZF2 Service Manager
Into the ZF2 Service Manager
Chris Tankersley
 
Codeigniter
Codeigniter
shadowk
 
CodeIgniter - PHP MVC Framework by silicongulf.com
CodeIgniter - PHP MVC Framework by silicongulf.com
Christopher Cubos
 
Codeigniter : the security and the magic of hook
Codeigniter : the security and the magic of hook
Abdul Malik Ikhsan
 
Modular PHP Development using CodeIgniter Bonfire
Modular PHP Development using CodeIgniter Bonfire
Jeff Fox
 
Ad

Recently uploaded (20)

Securing AI - There Is No Try, Only Do!.pdf
Securing AI - There Is No Try, Only Do!.pdf
Priyanka Aash
 
GenAI Opportunities and Challenges - Where 370 Enterprises Are Focusing Now.pdf
GenAI Opportunities and Challenges - Where 370 Enterprises Are Focusing Now.pdf
Priyanka Aash
 
WebdriverIO & JavaScript: The Perfect Duo for Web Automation
WebdriverIO & JavaScript: The Perfect Duo for Web Automation
digitaljignect
 
From Manual to Auto Searching- FME in the Driver's Seat
From Manual to Auto Searching- FME in the Driver's Seat
Safe Software
 
Coordinated Disclosure for ML - What's Different and What's the Same.pdf
Coordinated Disclosure for ML - What's Different and What's the Same.pdf
Priyanka Aash
 
AI Agents and FME: A How-to Guide on Generating Synthetic Metadata
AI Agents and FME: A How-to Guide on Generating Synthetic Metadata
Safe Software
 
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Saikat Basu
 
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
caoyixuan2019
 
Daily Lesson Log MATATAG ICT TEchnology 8
Daily Lesson Log MATATAG ICT TEchnology 8
LOIDAALMAZAN3
 
"Scaling in space and time with Temporal", Andriy Lupa.pdf
"Scaling in space and time with Temporal", Andriy Lupa.pdf
Fwdays
 
You are not excused! How to avoid security blind spots on the way to production
You are not excused! How to avoid security blind spots on the way to production
Michele Leroux Bustamante
 
Enhance GitHub Copilot using MCP - Enterprise version.pdf
Enhance GitHub Copilot using MCP - Enterprise version.pdf
Nilesh Gule
 
The Future of Technology: 2025-2125 by Saikat Basu.pdf
The Future of Technology: 2025-2125 by Saikat Basu.pdf
Saikat Basu
 
Lessons Learned from Developing Secure AI Workflows.pdf
Lessons Learned from Developing Secure AI Workflows.pdf
Priyanka Aash
 
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
yosra Saidani
 
“MPU+: A Transformative Solution for Next-Gen AI at the Edge,” a Presentation...
“MPU+: A Transformative Solution for Next-Gen AI at the Edge,” a Presentation...
Edge AI and Vision Alliance
 
OpenACC and Open Hackathons Monthly Highlights June 2025
OpenACC and Open Hackathons Monthly Highlights June 2025
OpenACC
 
Curietech AI in action - Accelerate MuleSoft development
Curietech AI in action - Accelerate MuleSoft development
shyamraj55
 
The Future of Product Management in AI ERA.pdf
The Future of Product Management in AI ERA.pdf
Alyona Owens
 
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik
 
Securing AI - There Is No Try, Only Do!.pdf
Securing AI - There Is No Try, Only Do!.pdf
Priyanka Aash
 
GenAI Opportunities and Challenges - Where 370 Enterprises Are Focusing Now.pdf
GenAI Opportunities and Challenges - Where 370 Enterprises Are Focusing Now.pdf
Priyanka Aash
 
WebdriverIO & JavaScript: The Perfect Duo for Web Automation
WebdriverIO & JavaScript: The Perfect Duo for Web Automation
digitaljignect
 
From Manual to Auto Searching- FME in the Driver's Seat
From Manual to Auto Searching- FME in the Driver's Seat
Safe Software
 
Coordinated Disclosure for ML - What's Different and What's the Same.pdf
Coordinated Disclosure for ML - What's Different and What's the Same.pdf
Priyanka Aash
 
AI Agents and FME: A How-to Guide on Generating Synthetic Metadata
AI Agents and FME: A How-to Guide on Generating Synthetic Metadata
Safe Software
 
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Saikat Basu
 
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
caoyixuan2019
 
Daily Lesson Log MATATAG ICT TEchnology 8
Daily Lesson Log MATATAG ICT TEchnology 8
LOIDAALMAZAN3
 
"Scaling in space and time with Temporal", Andriy Lupa.pdf
"Scaling in space and time with Temporal", Andriy Lupa.pdf
Fwdays
 
You are not excused! How to avoid security blind spots on the way to production
You are not excused! How to avoid security blind spots on the way to production
Michele Leroux Bustamante
 
Enhance GitHub Copilot using MCP - Enterprise version.pdf
Enhance GitHub Copilot using MCP - Enterprise version.pdf
Nilesh Gule
 
The Future of Technology: 2025-2125 by Saikat Basu.pdf
The Future of Technology: 2025-2125 by Saikat Basu.pdf
Saikat Basu
 
Lessons Learned from Developing Secure AI Workflows.pdf
Lessons Learned from Developing Secure AI Workflows.pdf
Priyanka Aash
 
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
yosra Saidani
 
“MPU+: A Transformative Solution for Next-Gen AI at the Edge,” a Presentation...
“MPU+: A Transformative Solution for Next-Gen AI at the Edge,” a Presentation...
Edge AI and Vision Alliance
 
OpenACC and Open Hackathons Monthly Highlights June 2025
OpenACC and Open Hackathons Monthly Highlights June 2025
OpenACC
 
Curietech AI in action - Accelerate MuleSoft development
Curietech AI in action - Accelerate MuleSoft development
shyamraj55
 
The Future of Product Management in AI ERA.pdf
The Future of Product Management in AI ERA.pdf
Alyona Owens
 
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik
 
Ad

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