should I use an abstract class or an interface?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
robasc33
Forum Newbie
Posts: 3
Joined: Tue Nov 15, 2011 12:03 pm

should I use an abstract class or an interface?

Post by robasc33 »

Hello everyone,

I am working on a design which will help me reduce the amount of code I am going to have to write. The overall outlook is this:

1. Design a way to format and send an email in a pdf, html, flash, etc.., using one method.

This would include the need of the Mail.php class as well as Mail_mime.php and some others, so I thought of creating either an abstract class name "Email" (which would have all associated variables which any instance of an email class could reuse) or use an interface that would extend Mail.php, Mail_mime.php, etc...

Using abstract class: if I use an abstract method I could reuse common variables and create an abstract method such as "sendEmail". From this I could create child classes such as HtmlEmail, FlashEmail, PdfEmail, etc... But this would increase the coupling due to having to include each class needed to format each specific type in the child classes.

Using Interface: By using the interface I could inherit from all needed classes such as Mail.php, Mail_mime.php, etc.., then create a method "sendEmail" so all child classes could use this. This would reduce coupling but would not allow me to reuse the common variables that each child class uses.

I am really looking for the best approach either an interface or an abstract class. what does everyone else think?
Post Reply