简体   繁体   中英

PHP abstract class vs interface (performance)

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?

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 eg 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 (eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM