views:

82

answers:

3

In PHP 5 and later what's faster - an interface or abstract class ?

abstract class MyPluginModel {

vs

interface MyPluginModel {

Any ideas how they compare in terms of memory and time performance?

+1  A: 

interface and abstract class are not comparable from performance point of view. It is indeed impossible to say how much memory an abstract or interface type will hold since they both only hold references of any concrete object that inherits them down the inheritance hierarchy.

this. __curious_geek
"how much memory an abstract or interface type will hold since they both only hold references of any concrete object that inherits them down the inheritance hierarchy" – I have an issue with this statement; the definition itself takes space – probably more than any object, except for objects that hold a lot of data.
Artefacto
You're right. But it will vary for each type that implements the interface or inherits from the abstract type.
this. __curious_geek
+8  A: 

It doesn't make sense to compare the two based solely on performance, as they're not the same thing (the "interface vs. abstract class" question has been posted in this site several times – see e.g. here and here –, so I'm not going to reiterate the argument).

That said, an interface is very likely going to be faster since there are less things to be done (e.g. it's not necessary to copy the instance properties from the superclass to the subclass). In the real world, the difference is, however, very unlikely to be noticed (even less than noticed if you're using an opcode cache, which you should, in production).

For further information, compare: zend_do_implement_interface, zend_do_inheritance.

As to memory, there shouldn't be any significant difference, both interfaces and abstract classes use the same data structure.

Artefacto
+4  A: 

The famous quote, "We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil", by Donald Knuth.

I really think you are doing premature optimization when you think about that.

Alfred