DEPENDENCY INJECTION with PHP
Nikita Groshin / Nguyễn Huy Tuấn
More than 200 employeesMore than 80 developers from different countriesDifferent programming languages and paradigmJava, C++, PHP, JS, Python etc
Cốc Cốc
13 membersPHP, JS, PythonKohana, Zend Framework100% Zend Certificated developers
Frontend and Tools Deprtment
Search engine http://coccoc.vnBrowser http://corom.vnNot only
Core Products
NO. They are USEFUL!Hard code is suck
Dependencies are suck?
Good Dependency
User_Model Article_Model
Dependency
Bad Dependency
User_Model Session
Dependency
Bad
Better
Good
Dependency Injection is bad
High LevelModule
Low LevelModule
Dependency
Good
High LevelModule
Low LevelModule
Abstraction
Constructor injectionSetter injectionInterface injection
Injection types
Interface Injection
Quite easy But why to use it?
Hard to reusePoor isolationHard to testA lot of codeHard to maintainHard to understand
Hard coded dependency
Loose couplingReuse of codeReduce amount of codeClear and UnderstandableExtremely easy to test
Dependency injection
Loose coupling
Reuse of code
Reduce amount of code
Clear and Understandable
Extremely easy to test
Dependency injection
Extremely easy to test
Manual Di management is complicated
We need container!
Disclaimer
Dependency Injection != DI containerDI container use Service Locator Anti-PatternDI container make code 'less native'Powerful tool – use carefulOften Manual injection is enough
How many lines of code we need for simple
DI container?
Twittee
http://twittee.org/ Packed in less than 140 characters, it fits in a tweet. Author Fabien Potencier Production ready
Twittee source
Pimple
http://pimple.sensiolabs.org/50 lines of codeFull functionality Author Fabien PotencierProduction ready
Questions?
[email protected]@coccoc.vn