Dependency injection with PHP

30
Dependency Injection with PHP Nikita Groshin Nguyễn Huy Tuấn

description

Speaker: Nikita Work at: CocCoc

Transcript of Dependency injection with PHP

Page 1: Dependency injection with PHP

Dependency Injection with PHPNikita Groshin

Nguyễn Huy Tuấn

Page 2: Dependency injection with PHP

Itim

More than 200 employees More than 80 developers from different

countries Different programming languages and paradigm Java, C++, PHP, JS, Python etc

Page 3: Dependency injection with PHP

Frontend and Tools Deprtment

13 members PHP, JS, Python Kohana, Zend Framework 100% Zend Certificated developers

Page 4: Dependency injection with PHP

Core Products

Search engine http://coccoc.vn/ Browser http://corom.vn Not only

Page 5: Dependency injection with PHP

"Developers! Developers! Developers"

"Dependency! Dependency! Dependency"

Page 6: Dependency injection with PHP

Dependencies are suck?

NO. They are useful! Hard code is suck

Page 7: Dependency injection with PHP

Good Dependency

Page 8: Dependency injection with PHP

Bad Dependency

Page 9: Dependency injection with PHP

Bad

Page 10: Dependency injection with PHP

Better

Page 11: Dependency injection with PHP

Good

Page 12: Dependency injection with PHP

Dependency Injection are simple.

Page 13: Dependency injection with PHP

Bad

Page 14: Dependency injection with PHP

Good

Page 15: Dependency injection with PHP

Injection types

Constructor injection Setter injection Interface injection

Page 16: Dependency injection with PHP

Interface Injection

Page 17: Dependency injection with PHP

Interface injection

Page 18: Dependency injection with PHP

Quite easy.But why to use it?

Page 19: Dependency injection with PHP

Hard coded dependency

Hard to reuse Poor isolation Hard to test A lot of code Hard to maintain Hard to understand

Page 20: Dependency injection with PHP

Dependency injection

Loose coupling Reuse of code Reduce amount of code Clear and Understandable Extremely easy to test

Page 21: Dependency injection with PHP

Dependency injection

Loose coupling

Reuse of code

Reduce amount of code

Clear and Understandable

Extremely easy to test

Page 22: Dependency injection with PHP

Extremely easy to test

Page 23: Dependency injection with PHP

Manual Di management is complicated.

Page 24: Dependency injection with PHP

We need container!

Page 25: Dependency injection with PHP

Disclaimer

Dependency Injection != DI container DI container use Service Locator Anti-Pattern DI container make code 'less native' Powerful tool – use careful Often Manual injection is enough

Page 26: Dependency injection with PHP

How many lines of codewe need for simple

DI container?

Page 27: Dependency injection with PHP

Twittee

http://twittee.org/ Packed in less than 140 characters, it fits in a

tweet. Author Fabien Potencier Production ready

Page 28: Dependency injection with PHP

Twittee source

Page 29: Dependency injection with PHP

Pimple

http://pimple.sensiolabs.org/ 50 lines of code Full functionality Author Fabien Potencier Production ready

Page 30: Dependency injection with PHP

Questions?

job.coccoc.vn [email protected]