Recipe 7.8. Creating Abstract Base Classes

7.8.1. Problem

You want to create an "abstract" class, or, in other words, one that is not directly instantiable, but acts as a common base for children classes.

7.8.2. Solution

Label the class as abstract:

abstract class Database {   // ... }

Do this by placing the abstract keyword before the class definition.

You must also define at least one abstract method in your class. Do this by placing the abstract keyword in front of the method definition:

abstract class Database {     abstract public function connect();     abstract public function query();     abstract public function fetch();     abstract public function close(); }

7.8.3. Discussion

Abstact classes are best used when you have a series of objects that are related using the "is a" relationship. Therefore, it makes logical sense to have them descend from a common parent. However, while the children are tangible, the parent is abstract.

Take, for example, a Database class. A database is a real object, so it makes sense to have a Database class. However, although Oracle, MySQL, Postgres, MSSQL, and hundreds of other databases exist, you cannot download and install a generic database. You must choose a specific database.

PHP provides a way for you to create a class that cannot be instantiated. This class is known as an abstract class. For example, see the Database class in Example 7-24.

Defining an abstract class

abstract class Database {     abstract public function connect();     abstract public function query();     abstract public function fetch();     abstract public function close(); }

Mark a class as abstract by placing the abstract keyword before class.

Abstract classes must contain at least one method that is also marked abstract. These methods are called abstract methods. Database contains four abstract methods: connect( ), query( ), fetch( ), and close( ). These four methods are the basic set of functionality necessary to use a database.

If a class contains an abstract method, the class must also be declared abstract. However, abstract classes can contain non-abstract methods (even though there are no regular methods in Database).

Abstract methods, like methods listed in an interface, are not implemented inside the abstract class. Instead, abstract methods are implemented in a child class that extends the abstract parent. For instance, you could use a MySQL class, as shown in Example 7-25.

Implementing a class based on an abstract class

class MySQL extends Database {     protected $dbh;     protected $query;     public function connect($server, $username, $password, $database) {         $this->dbh = mysqli_connect($server, $username,                                        $password, $database);     }     public function query($sql) {         $this->query = mysqli_query($this->dbh, $sql);     }     public function fetch() {         return mysqli_fetch_row($this->dbh, $this->query);     }     public function close() {         mysqli_close($this->dbh);     } }

If a subclass fails to implement all the abstract methods in the parent class, then it itself is abstract and another class must come along and further subclass the child. You might do this if you want to create two MySQL classes: one that fetches information as objects and another that returns arrays.

There are two requirements for abstract methods:

  • Abstract methods cannot be defined private, because they need to be inherited.

  • Abstract methods cannot be defined final, because they need to be overridden.

Abstract classes and interfaces are similar concepts, but are not identical. For one, you can implement multiple interfaces, but extend only one abstract class. Additionally, in an interface you can only define method prototypes'you cannot implement them. An abstract class, in comparison, needs only one abstract method to be abstract, and can have many non-abstract methods and even properties.

You should also use abstract classes when the "is a" rule applies. For example, since you can say "MySQL is a Database," it makes sense for Database to be abstract class. In constrast, you cannot say, "Book is a Nameable" or "Book is a Name," so Nameable should be an interface.

PHP Cookbook, 2nd Edition
PHP Cookbook: Solutions and Examples for PHP Programmers
ISBN: 0596101015
EAN: 2147483647
Year: 2006
Pages: 445

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: