Using Abstract Methods In PHP

Abstract methods are special in that they are declared in a parent class, but they are implemented in child classes. They can help make a class serve as a model for child classes, for example, you could make an “employee” class with basic functions (abstract methods) like calculate_tax, fire, hire, etc. You could then make child classes like “clerk”, “cashier”, and “manager”, which implement those abstract methods and define what they do for that specific type of employee.

How Do I Declare Abstract Methods?

To use them you first have to make your class support abstract methods by declaring it as “abstract.” This is required, as regular classes do not support abstract methods. Once your class is abstract you can add in the needed functions. Here is a quick example:

abstract class db_link {
var $link;
var $db_user;
var $db_pass;

abstract function connect($user,$pass);
abstract function disconnect($link);
}

That class could serve as a basic database link. You could subclass it and define more specific classes that cover certain databases, like MySQL, MSSQL, and even flat file. All of the classes would have something in common - they have the same basic methods.

How Do I Implement Abstract Methods?

To implement them you have to first make a child class (just like you would normally do in PHP) and then just declare the function as you normally would. Here is an example using the above db_link class as a parent class:

class mysql_link extends db_link{
function connect($user,$pass) {
//connect to mysql database
}

function disconnect($link) {
//disconnect from mysql database
}
}

Do take note that the children must implement all of the abstract methods, you can’t pick and choose which ones to use!

Huh, Isn’t This The Same Thing As Interfaces?

Abstract methods and class interfaces are very similar in functionality, but they do differ. Here are my general guidelines when choosing which one to use:

If you are going to create several unrelated classes with similar functions use an interface, if you are going to create several related classes then use abstract methods.

If the behavior of the functions will be similar, use abstract methods, if not use interfaces.

If you are planning to inherit multiple sets of functions from different classes, use an interface. PHP can inherit methods from multiple interfaces, but it can only extend one abstract class.

There are also many other little rules to choosing which way to go. As always, Google is your friend.

Some Side Notes

If you are planning on creating a wrapper class (a database class, for example) you should definitely use abstract methods. It will save you lots of valuable time, and I am sure it will make your script run a bit faster as well.

You can define abstract methods as being private, public, or protected as well. To do that simply type “abstract public…” or private, etc.

Also, don’t forget, you have to implement any abstract methods you declare or else you will get errors galore!

Please subscribe, or else I will cry. Do you really want to make a programmer cry?

Leave a Reply

Note: By submitting your comment you agree to this blog's comment policy.

If you want a little icon next to your name - sign up for one at Gravatar.