InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Creating Abstract Base Classes in PHP
PHP

Creating Abstract Base Classes in PHP

InfinityCoder November 24, 2016

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.

Label the class as abstract:

1
2
3
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:

1
2
3
4
5
6
abstract class Database {
   abstract public function connect();
   abstract public function query();
   abstract public function fetch();
   abstract public function close();
}

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:

1
2
3
4
5
6
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: con
nect(), 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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Reading a File into a String in PHP
You want to load the entire contents of a file …

Reading a File into a String in PHP

Reading Cookie Values in PHP
You want to read the value of a cookie that …

Reading Cookie Values in PHP

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap