oop - Polymorphism vs dependency injection in PHP? -



oop - Polymorphism vs dependency injection in PHP? -

what difference between polymorphism , dependency injection in php? me seem same thing.

polymorphism provision of single interface entities of different types. means define 1 parent class, i.e. person , derive multiple other classes off of it. i.e. mailman, programmer, dentist. these kid classes have in mutual person, implement specialized functions well.

dependency injection software design pattern implements inversion of command , allows programme design follow dependency inversion principle. term coined martin fowler. injection passing of dependency (a service) dependent object (a client). database illustration of this. let's our person class above needs persist info itself. dependency injection involve passing database object person class work with. person class doesn't worry how database persists information, concerned public api of database. swap out databases , long apis same, person class not care. becomes handy when want unit test classes , need remove dependency on database. can utilize dependency injection pass in mock database returns dummy information.

here 2 previous stackoverflow questions related each:

what polymorphism, for, , how used?

what dependency injection?

also, check out martin fowler's site more info of this. http://www.martinfowler.com/articles/injection.html

php oop dependency-injection polymorphism

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -