In PHP 5 and later what's faster - an interface
or abstract class
?
a开发者_StackOverflow社区bstract class MyPluginModel {
vs
interface MyPluginModel {
Any ideas how they compare in terms of memory and time performance?
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.
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.
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.
精彩评论