speaker nguyen huy tuan dependency injection in php

27
DEPENDENCY INJECTION with PHP Nikita Groshin / Nguyễn Huy Tuấn

Upload: aiti-education

Post on 18-Dec-2014

671 views

Category:

Documents


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Speaker nguyen huy tuan   dependency injection in php

DEPENDENCY INJECTION with PHP

Nikita Groshin / Nguyễn Huy Tuấn

Page 2: Speaker nguyen huy tuan   dependency injection in php

More than 200 employeesMore than 80 developers from different countriesDifferent programming languages and paradigmJava, C++, PHP, JS, Python etc

Cốc Cốc

Page 3: Speaker nguyen huy tuan   dependency injection in php

13 membersPHP, JS, PythonKohana, Zend Framework100% Zend Certificated developers

Frontend and Tools Deprtment

Page 4: Speaker nguyen huy tuan   dependency injection in php

Search engine http://coccoc.vnBrowser http://corom.vnNot only

Core Products

Page 5: Speaker nguyen huy tuan   dependency injection in php

NO. They are USEFUL!Hard code is suck

Dependencies are suck?

Page 6: Speaker nguyen huy tuan   dependency injection in php

Good Dependency

User_Model Article_Model

Dependency

Page 7: Speaker nguyen huy tuan   dependency injection in php

Bad Dependency

User_Model Session

Dependency

Page 8: Speaker nguyen huy tuan   dependency injection in php

Bad

Page 9: Speaker nguyen huy tuan   dependency injection in php

Better

Page 10: Speaker nguyen huy tuan   dependency injection in php

Good

Page 11: Speaker nguyen huy tuan   dependency injection in php

Dependency Injection is bad

High LevelModule

Low LevelModule

Dependency

Page 12: Speaker nguyen huy tuan   dependency injection in php

Good

High LevelModule

Low LevelModule

Abstraction

Page 13: Speaker nguyen huy tuan   dependency injection in php

Constructor injectionSetter injectionInterface injection

Injection types

Page 14: Speaker nguyen huy tuan   dependency injection in php

Interface Injection

Page 15: Speaker nguyen huy tuan   dependency injection in php

Quite easy But why to use it?

Page 16: Speaker nguyen huy tuan   dependency injection in php

Hard to reusePoor isolationHard to testA lot of codeHard to maintainHard to understand

Hard coded dependency

Page 17: Speaker nguyen huy tuan   dependency injection in php

Loose couplingReuse of codeReduce amount of codeClear and UnderstandableExtremely easy to test

Dependency injection

Page 18: Speaker nguyen huy tuan   dependency injection in php

Loose coupling

Reuse of code

Reduce amount of code

Clear and Understandable

Extremely easy to test

Dependency injection

Page 19: Speaker nguyen huy tuan   dependency injection in php

Extremely easy to test

Page 20: Speaker nguyen huy tuan   dependency injection in php

Manual Di management is complicated

Page 21: Speaker nguyen huy tuan   dependency injection in php

We need container!

Page 22: Speaker nguyen huy tuan   dependency injection in php

Disclaimer

Dependency Injection != DI containerDI container use Service Locator Anti-PatternDI container make code 'less native'Powerful tool – use carefulOften Manual injection is enough

Page 23: Speaker nguyen huy tuan   dependency injection in php

How many lines of code we need for simple

DI container?

Page 24: Speaker nguyen huy tuan   dependency injection in php

Twittee

http://twittee.org/ Packed in less than 140 characters, it fits in a tweet. Author Fabien Potencier Production ready

Page 25: Speaker nguyen huy tuan   dependency injection in php

Twittee source

Page 26: Speaker nguyen huy tuan   dependency injection in php

Pimple

http://pimple.sensiolabs.org/50 lines of codeFull functionality Author Fabien PotencierProduction ready

Page 27: Speaker nguyen huy tuan   dependency injection in php

Questions?

[email protected]@coccoc.vn