PHP interfaces and abstract classes

They have: 426 posts

Joined: Feb 2005

I am still trying to get my head around php interfaces and abstract classes.

OK, so it provides a kind of framework or standard for other people to implement, but what is the point.

Does it provide any performance improvements?
When would you need to use them; if at all?
Does it matter for the sake of good OOP that you dont use them?

Any advice appreciated

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

OK, so it provides a kind of framework or standard for other people to implement, but what is the point.

From what I know, it was to enforce how your class is to be used then inherited/extended. I don't think there is a point other than that.

It doesn't do any performance improvements that I know of, and no, I have not had any use for them. Maybe if I coded with a team, but I usually do not.

They have: 121 posts

Joined: Dec 2008

I often use interfaces to group like behavior on similar objects so the 'manager' class may expect only an instance of the interface, rather than a full class.

A Car and a Bicycle should both move(SomeType direction) in terms of being objects in traffic, and so perhaps implement the 'iMoveable' interface

Now a TrafficCop object may have the method addEntity(iMoveable object), instead of having an addCar(Car theCar) method and a addBicycle(Bicycle aBike) method. It can do this because all TrafficCop needs to know is that objects it obtains through the addEntity method implement a method called 'move(SomeType direction)'

And, later on, when you realize you forgot pedestrians in your implementation, no changes are required in your TrafficCop implementation... If city planners could only design to interfaces! Laughing out loud.

Abstract classes allow for a partial implementation of an object, so other objects may extend the abstract, without the implementer having to worry that someone may try to instantiate the partial implementation...

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.