Upload
abdul-malik-ikhsan
View
1.796
Download
0
Tags:
Embed Size (px)
DESCRIPTION
Zend Framework 2 : Dependency Injection
Citation preview
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 ZEND\DI
<?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 Zend\Di\Di; $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 \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();
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