0

What is the difference between polymorphism and dependency injection in PHP? To me they seem like the same thing.

Mantas D
  • 3,400
  • 3
  • 21
  • 24

3 Answers3

3

Polymorphism is the provision of a single interface to entities of different types. This means that you define one parent class, i.e. Person and derive multiple other classes off of it. I.E. Mailman, Programmer, Dentist. These child classes all have something in common with person, but they implement specialized functions as well.

Dependency Injection is a software design pattern that implements inversion of control and allows a program design to follow the dependency inversion principle. The term was coined by Martin Fowler. An injection is the passing of a dependency (a service) to a dependent object (a client). A database is a good example of this. Let's say our person class above needs to persist some data about itself. Dependency injection would involve passing a database object into the Person class to work with. The Person class doesn't worry about how the database persists its information, it is only concerned with the public api of the database. You could effectively swap out databases and as long as their apis are the same, the Person class would not care. This becomes very handy when you want to unit test your classes and need to remove the dependency on the database. You can use dependency injection to pass in a mock database that always returns dummy information.

Here are two previous stackoverflow questions related to each:

What is polymorphism, what is it for, and how is it used?

What is dependency injection?

Also, check out Martin Fowler's site for more information of this. http://www.martinfowler.com/articles/injection.html

Community
  • 1
  • 1
Tanner
  • 211
  • 2
  • 8
  • DI and IoC doesn't use abstract class and also looks more like a wrapper or decorator. But it's bling bling buzzword for something 5 pence. – Gigamegs Oct 20 '14 at 22:51
  • While I agree that DI and IoC don't use abstract classes, I disagree that they are wrappers or decorators. You are simply "injecting" the dependency into the object, via a constructor or setter methods, instead of the dependent object being responsible for the dependencies' creation. You're passing the responsibility up the chain of responsibility. I can agree that it is a buzzword of sorts, but what it gives you in the way of unit testing and flexibility is worth a bit of buzz in my opinion. – Tanner Oct 21 '14 at 03:12
  • :I cited you "instead of the dependent object being responsible for the dependencies' creation. You're passing the responsibility up the chain of responsibility." Isn't this 2 totally different? – Gigamegs Oct 21 '14 at 11:02
  • I don't think they are different. Let's say a Person class needs a database object. Instead of the Person class calling database = new Database() during instantiation of the Person class, the database is passed into the Person class through the person class's constructor or via a setter method. I'm not real sure I'm fully understanding your problem with my answer? – Tanner Oct 21 '14 at 12:59
  • I'm not sure if I understand you but in wrappers the object isn't created either in the class. IMO it's not what you give in your answer. Also isn't this an old subject? The article is from 2008? I don't think I have used something else then a wrapper/DI or IoC?? I agree with you with the buzzwords thing. – Gigamegs Oct 21 '14 at 13:02
  • I guess you could use dependency injection like a wrapper, but that's not the intent of the pattern as I understand it. An adapter/wrapper is meant to "convert the interface of a class into another interface clients expect" (p.139 Design Patters, Gang of Four) While dependency injection is used for decoupling and reducing connasence. – Tanner Oct 21 '14 at 13:19
  • :Can you point us to a source code with a proper example of dependency injection and/or Inversion of Control? – Gigamegs Oct 24 '14 at 12:22
  • @Phpdna [Here](http://code.tutsplus.com/tutorials/dependency-injection-in-php--net-28146) is a great blog post on DI and why it's important in php. Also, [Laravel's IoC Container](http://laravel.com/docs/4.2/ioc) is an example of DI being used in the wild. – Tanner Oct 24 '14 at 13:51
1

I think dependency injection is much simpler. Its like to inject a class(object) and call a parent method. Similar to a wrapper. Polymorphism uses abstract classes and it let you define non-existance functions.

Gigamegs
  • 12,342
  • 7
  • 31
  • 71
0

How I understand it now:

polymorphism + injected object = dependency injection

Polymorphism - is when you create objects that implement the same interface, so all objects have the same base methods.

Dependency injection - you inject object, that can be swapped with other object. But all those object implement the same interface (like in polymorphism).

Nandakumar V
  • 3,563
  • 2
  • 22
  • 44
Mantas D
  • 3,400
  • 3
  • 21
  • 24
  • Yes, that is correct. The whole point is you don't lock the object that needs the dependency into instantiating the dependency so that you can later swap in a stub/mock object for testing. Or for example if you wanted to switch between two different databases, i.e. mysql/postgres. In order to do this you usually will need an adapter/driver to ensure a compatible interface. Php's PDO class is a great example of this. It acts as a driver for the different databases so that you can swap them out. – Tanner Oct 21 '14 at 12:42
  • @Mantas D:Polymorphism is much more complex.It uses abstract classes and non existing functions. I think DI and IoC isn't so useful like it appear. – Gigamegs Oct 22 '14 at 12:43