PHP Tutorial

PHP Flow Control

PHP Functions

PHP String

PHP Array

PHP Date Time

PHP Object Oriented

Regular Expression

PHP Cookie & Session

PHP Error & Exception handling

MySQL in PHP

PHP File Directory

PHP Image Processing

PHP Abstract Classes And Abstract Methods

Abstract classes and methods are an important part of object-oriented programming in PHP. They're used to define and enforce rules for classes that inherit from them.

Abstract Classes

In PHP, an abstract class is a class that cannot be instantiated on its own. It serves as a blueprint for other classes. To define an abstract class, use the abstract keyword:

abstract class AbstractClass {
    // Class body
}

An abstract class can contain both regular methods (those with a body) and abstract methods (which are declared, but not implemented in the abstract class).

abstract class AbstractClass {
    public function regularMethod() {
        // implementation here
    }

    abstract protected function abstractMethod();
}

Abstract Methods

Abstract methods are declared in an abstract class, but they don't have any implementation within the abstract class. The implementation is provided in the classes that inherit from the abstract class.

When a class extends an abstract class, it must provide an implementation for all of the abstract methods in the parent class. If it doesn't, the child class must also be declared as abstract.

Here's an example:

abstract class AbstractClass {
    abstract protected function prefixName($name);
}

class ConcreteClass extends AbstractClass {
    // Our child class may define optional parameters not in parent's signature
    public function prefixName($name, $separator = ".") {
        if ($name == "Pacman") {
            $prefix = "Mr";
        } elseif ($name == "Pacwoman") {
            $prefix = "Mrs";
        } else {
            $prefix = "";
        }
        return "{$prefix}{$separator} {$name}";
    }
}

$class = new ConcreteClass;
echo $class->prefixName("Pacman"), "\n";
echo $class->prefixName("Pacwoman"), "\n";

In the example above, AbstractClass provides an abstract method called prefixName(). ConcreteClass then provides an implementation for prefixName(). Because ConcreteClass provides an implementation for all abstract methods in the parent class, it can be instantiated.

The primary purpose of abstract classes and methods is to provide a common interface for its child classes. It's a way to enforce certain methods that all the child classes must implement. Abstract classes cannot be instantiated, they can only be subclassed.

  1. PHP abstract class example:

    • An abstract class cannot be instantiated on its own. It serves as a blueprint for other classes.
    abstract class Shape {
        abstract public function calculateArea();
    }
    
  2. PHP abstract class vs interface:

    • While both abstract classes and interfaces define blueprints, abstract classes can have implemented methods, while interfaces cannot.
    abstract class Shape {
        abstract public function calculateArea();
    }
    
    interface Drawable {
        public function draw();
    }
    
  3. PHP abstract method example:

    • Abstract methods have no implementation in the abstract class and must be implemented by derived classes.
    abstract class Shape {
        abstract public function calculateArea();
    }
    
  4. PHP abstract class constructor:

    • Abstract classes can have constructors that are called when an instance of a concrete class is created.
    abstract class Animal {
        protected $name;
    
        public function __construct($name) {
            $this->name = $name;
        }
    
        abstract public function makeSound();
    }
    
  5. PHP abstract class inheritance:

    • Abstract classes can be extended, and abstract methods must be implemented in the child class.
    abstract class Shape {
        abstract public function calculateArea();
    }
    
    class Circle extends Shape {
        public function calculateArea() {
            // Implementation for calculating the area of a circle
        }
    }
    
  6. PHP abstract class use cases:

    • Use abstract classes when you want to provide a common base with some shared functionality for derived classes.
    abstract class DatabaseConnection {
        abstract protected function connect();
    
        public function query($sql) {
            $connection = $this->connect();
            // Execute the query
        }
    }
    
  7. PHP abstract method visibility:

    • Abstract methods must have the same or weaker visibility in the child class.
    abstract class Shape {
        abstract protected function calculateArea();
    }
    
    class Circle extends Shape {
        protected function calculateArea() {
            // Implementation for calculating the area of a circle
        }
    }
    
  8. PHP abstract class and interface together:

    • A class can extend one abstract class and implement multiple interfaces.
    abstract class Shape {
        abstract public function calculateArea();
    }
    
    interface Drawable {
        public function draw();
    }
    
    class Circle extends Shape implements Drawable {
        public function calculateArea() {
            // Implementation for calculating the area of a circle
        }
    
        public function draw() {
            // Implementation for drawing a circle
        }
    }