Broad Network


Abstract Base Class in PHP

Object Oriented Programming in PHP – Part 5

Forward: In this article I explain the operation of the abstract base class in PHP.

By: Chrysanthus Date Published: 9 Aug 2012

Introduction

This is part 5 of my series, Object Oriented Programming in PHP. In programming, you can design a program and then some day in the future another person will add something to the program. The focus here is to design a class, whereby there are one or more methods without definition (blocks). Someday in the future somebody will inherit a class form it and put a definition for the method (or methods) of the inherited class. Such a class that has one or more methods without definition is called an abstract base class. To give the class method a definition, you have to inherit a class from it and then in the derived class you give the definition to the method (function). You will then instantiate and use objects from the derived class and not objects from the base class (abstract base class). In this article I explain the operation of the abstract base class in PHP.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

An Abstract Base Class
An abstract base class is a class with what is known as a virtual function. A virtual function is a function (method) that does not have an implementation (that is, it does not have a definition; in other words it does not have a function body in curly braces). Well, you need to know how to use this virtual function (method). The typing has a declaration that is preceded by the reserved word, abstract. The declaration of the method is the method without the method block, ending with a semi colon. The story does not end there: you precede an abstract base class with the word, abstract. You can have more than one virtual function in an abstract base class. The following code illustrates the description of an abstract class with a virtual function.

<?php

    abstract class MyClass
        {

            abstract public function absMthd();

        }

?>

The class has just one method; it is the virtual function. Note the way it has been typed. It begins with the word, abstract; note that the word also precedes the class description. Any class with a virtual function is called an abstract base class. In future, you can inherit other classes from this class and in the inherited (derived) classes you would give the method its definition block.

An abstract base class can have completely defined methods as well as abstract methods; however, it can never be used to instantiate an object, you can only inherit a class from it.

Defining a Virtual Function through Inheritance
In the following code the base class is an abstract base class.

<?php

    abstract class MyClass
        {

            abstract public function absMthd();

        }

    class MyChildClass extends MyClass
        {
            public function absMthd()
                {
                    echo "I have been defined in the derived class.";
                }
        }

    $ChObj = new MyChildClass();
    $ChObj->absMthd();

?>

Read and try the above code. The parent class is an abstract base class. A practical base class will have other properties and methods. You then have the inherited class. You defined the virtual function in the inherited class in the same way that you would define other methods, but with the same name that is in the base class and without the preceding word, abstract.

Note: you cannot instantiate a class from a base abstract class, because it has one or more functions that are not implemented. You can instantiate an object from the corresponding derived class, where the virtual functions (methods) have been implemented.

That is what I have for abstract base class. Let us stop here and continue in the next part of the series.

Chrys

Related Links

Major in Website Design
Web Development Course
HTML Course
CSS Course
ECMAScript Course
PHP Course
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message