Broad Network


Abstract Methods and Abstract Classes in PHP

Classes and Objects in PHP - Part 6

Foreword: In this part of the series, I talk about Abstract Methods and Abstract Classes in PHP.

By: Chrysanthus Date Published: 24 Nov 2018

Introduction

This is part 6 of my series, Classes and Objects in PHP. In this part of the series, I talk about Abstract Methods and Abstract Classes in PHP. You should have read the previous parts of the series before coming here, as this is the continuation.

Reason for Abstract Class
You may write a class today; you know the method, but you have not yet decided on the method implementation. In that case you create the abstract class. In future you implement the method. In some cases, the method might even be implemented by a different person. It is also possible that with time, the implementation code may be changed; in that case, the base class structure is left intact and not affected.

I start by explaining the math formula for the area of a square and the math formula for the area of a rectangle. I will use the formula for the area of a square and the formula for the area of a rectangle to illustrate abstract features in PHP classes and objects.

Area of a Square
A square has 4 sides (edges) equal. The mathematics formula for the area of a square is given by,

    Area = side X side

Abbreviating Area of square to As and side to s, the formula becomes,

    As = s x s

If s is 3, then

      As = 3 X 3
=>  As = 9

Area of a Rectangle
A rectangle is similar to a square but has 2 opposite sides equal. The area of a rectangle is given by,

    Area = length X width

Abbreviating Area of rectangle to Ar and length to l and width to w, the formula becomes,

    Ar = l x w

If l is 5 and w is 2, then

      Ar = 5 X 2
=>  Ar = 10

Abstract Method
A function signature is only the first line of a function definition ending with a semicolon. An abstract method is the method signature in a class definition. The block of the definition is given in the derived class. In the following code, you have an abstract method to obtain the area of a rectangle. The full definition of the method is given in the derived class.

<?php

    abstract class FourSides
        {
            public function squareArea($s)
                {
                    $A = $s * $s;
                    return $A;
                }

            abstract public function rectangleArea($l, $w);
        }

    class Rectangle extends FourSides
        {
            public function rectangleArea($l, $w)
                {
                    $A = $l * $w;
                    return $A;
                }
        }

    $childObj = new Rectangle();

    $As = $childObj->squareArea(3);
    echo $As, '<br>';

    $Ar = $childObj->rectangleArea(5, 2);
    echo $Ar, '<br>';

?>

Note that in programming, X is *. Also note how the abstract method has been declared in the FourSides class. The output is:

    9
    10

Abstract Class
An abstract method in a class makes that class an abstract class, in the sense that the class cannot be instantiated (object created from it). To simplify things, just make the class an abstract class by preceding the class definition (description) with the word, abstract - there is some redundancy here. The following code shows how an abstract class is defined and used through the derived class:

<?php

    abstract class FourSides
        {
            abstract public function squareArea($s);
            abstract public function rectangleArea($l, $w);
        }

    class Rectangle extends FourSides
        {
            public function squareArea($s)
                {
                    $A = $s * $s;
                    return $A;
                }

            public function rectangleArea($l, $w)
                {
                    $A = $l * $w;
                    return $A;
                }
        }

    $childObj = new Rectangle();

    $As = $childObj->squareArea(3);
    echo $As, '<br>';

    $Ar = $childObj->rectangleArea(5, 2);
    echo $Ar, '<br>';

?>

The output is:

    9
    10

Note that in the derived class, the visibility of the abstract method must be the same or more. That is, if the visibility is protected, the child class method must have protected or public visibility; if the visibility is private, the child class must have private or protected or public visibility. Furthermore, the signatures of the methods must match, i.e. the type hints and the number of required arguments must be the same

That is it for this part of the series.

Chrys


Related Links

Basics of PHP with Security Considerations
White Space in PHP
PHP Data Types with Security Considerations
PHP Variables with Security Considerations
PHP Operators with Security Considerations
PHP Control Structures with Security Considerations
PHP String with Security Considerations
PHP Arrays with Security Considerations
PHP Functions with Security Considerations
PHP Return Statement
Exception Handling in PHP
Variable Scope in PHP
Constant in PHP
PHP Classes and Objects
Reference in PHP
PHP Regular Expressions with Security Considerations
Date and Time in PHP with Security Considerations
Files and Directories with Security Considerations in PHP
Writing a PHP Command Line Tool
PHP Core Number Basics and Testing
Validating Input in PHP
PHP Eval Function and Security Risks
PHP Multi-Dimensional Array with Security Consideration
Mathematics Functions for Everybody in PHP
PHP Cheat Sheet and Prevention Explained
More Related Links

Cousins

BACK

Comments