View Full Version : What's the benefit of abstract classes and interfaces?

08-06-2008, 01:04 AM
Hi there,

I've just read the PHP's manual abstract classes and interface chapters. I still wonder why should I use them? What's the benefit of using abstract classes or using interfaces?

08-06-2008, 05:27 AM
Both of these serve great purposes. Its more difficult to explain in a language like PHP since it is datatype weak, but I'll try nonetheless.

An abstract is just like a class. Its like saying 'this is a skeleton for what is to be expected of any object for this type'. It can contain concrete methods for logic that does not change, or changes very little for any of the child classes. Child classes can override any of these methods (except in PHP 5.0.x, where Private children are overridden by their parents... [bug] grrr...). Abstract methods can also be included, which is the same as 'I know you're children will need this method, but its different for each child; let them worry about it'. Abstracts cannot be instantiated, but you can create final constructors if you want an abstract singleton pattern factory for example.

Since OOP logic dictates that a class can only extend one other class, interfaces are used to provide 'filler' methods for expected functionality. It is once again a skeleton, and simply says 'If you implement me, you need to include these methods'.

This is where it gets tricky in PHP explanation. Strong datatype languages like C++, C# and java declare their variables by telling it what type it requires. This allows the compilier or interpreter to allocate a certain amount of space in memory for this type of data. PHP needs to do these checks with keywords like instanceof or get_class/interface comparisons.

Consider the following:

interface IComparable
public function compareTo(IComparable $other);

class MyClass implements IComparable
private $key;
public function compareTo(IComparable $other)
// PHP doesn't support generics, so we need to check the type ourselves
$class = get_class($this); // Or just get_class() is fine.
if (!($other instanceof $class))
throw new Exception(sprintf("Class %s cannot be compared to type %s!\n", get_class(), get_class($other));
return (int)$this->key - (int)$other->key;

The interface serves the purpose of guaranteeing that compareTo exists on any object of type IComparable. In Java or C# you can store this as a type of IComprable:

public static void main(String args[])
IComparable i1 = new MyClass(1);
IComparable i2 = new MyClass(2);
System.out.println("i1.compareTo(i2)? " + i1.compareTo(i2));

If myclass cannot be stored as an IComparable, runtime will fail on the new MyClass calls, and if I'm not mistaken the compiler will fail on the i1.compareTo call.

I hope this helps. OOP takes a lot of practice to really determine how/what/when to use abstracts and interfaces. Good choices in UML Class design will really provide you with great flexibility in you're object designs.

08-06-2008, 02:40 PM
Wow thanks.. I still think I need to buy a book in Finnish :/

08-06-2008, 05:53 PM
Any OO concept book would work. Each language has their own rules, but the overall picture is pretty much the same.
I started natively as a procedural programmer with Perl, PHP and C. I moved into OO languages much later. It took me probably a year before I recognized the key benefits of using abstracts and interfaces - especially interfaces. One day I just started using them more and more, and now I pretty much use them everywhere. PHP will make me very happy once they support nested classes and object generics. Whether zend will do that is an entirely different matter, but I'm still keeping my fingers crossed for those.