PHP Classes and Objects Creating Abstract Base Classes - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript PHP Classes and Objects Creating Abstract Base Classes - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Saturday, May 25, 2019

PHP Classes and Objects Creating Abstract Base Classes

PHP Classes and Objects




Creating Abstract Base Classes

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.

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();
           }

Discussion

Abstract 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, whereas 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:

           abstract class Database {
                 abstract public function connect($server, $username, $password, $database);
                 abstract public function query($sql);
                 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 nonabstract 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:

           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);
                 }
           }

When implementing abstract methods, you must keep the same method prototypes. In this example, for instance, query() takes one argument, $sql.

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 nonabstract methods and even properties.

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


No comments:

Post a Comment

Post Top Ad