Answers for "php interface vs abstract class"

PHP
4

php interface vs abstract class

Use an interface when you want to force developers working in your 
system (yourself included) to implement a set number of methods on the 
classes they'll be building.
Use an abstract class when you want to force developers working in your 
system (yourself included) to implement a set numbers of methods and you 
want to provide some base methods that will help them develop their child 
classes.
Another thing to keep in mind is client classes can only extend one abstract 
class, whereas they can implement multiple interfaces. So, if you're 
defining your behavior contracts in abstract classes, that means each child 
class may only conform to a single contract. Sometimes this a good thing, 
when you want to force your user-programmers along a particular path. Other 
times it would be bad. Imagine if PHP's Countable and Iterator interfaces 
were abstract classes instead of interfaces.
One approach that's common when you're uncertain which way to go (as 
mentioned by cletus below) is to create an interface, and then have your 
abstract class implement that interface.
Posted by: Guest on October-30-2020

Code answers related to "php interface vs abstract class"

Browse Popular Code Answers by Language