PHP Abstract vs Class vs Interface
Syntax
Abstract Class
abstract class ClassName {
// properties and methods
}
Class
class ClassName {
// properties and methods
}
Interface
interface InterfaceName {
// method signatures
}
Explanation
PHP provides us with three ways of creating classes:
Abstract Class: it is a class that cannot be instantiated, it can only be inherited. It contains one or more abstract methods, which means that the child classes must provide implementation for these methods. Abstract classes can also contain non-abstract (concrete) methods and properties.
Class: it is a basic template for creating objects. It can contain properties and methods. Objects created from a class are instances of that class.
Interface: it is a contract that the implementing classes must adhere to. It only contains method signatures, without any implementation. A class can implement multiple interfaces, but it cannot extend multiple classes.
Example
Abstract Class
abstract class Shape {
protected $color;
public function setColor($color) {
$this->color = $color;
}
abstract public function getArea();
}
class Square extends Shape {
private $side;
public function __construct($side) {
$this->side = $side;
}
public function getArea() {
return $this->side * $this->side;
}
}
$square = new Square(5);
$square->setColor('red');
echo $square->getArea();
Class
class Car {
public $make;
public $model;
public $year;
public function __construct($make, $model, $year) {
$this->make = $make;
$this->model = $model;
$this->year = $year;
}
public function getMake() {
return $this->make;
}
}
$car = new Car('Ford', 'Mustang', 2021);
echo $car->getMake();
Interface
interface Animal {
public function makeSound();
}
class Dog implements Animal {
public function makeSound() {
echo 'Woof woof!';
}
}
$dog = new Dog();
$dog->makeSound();
Use
- Abstract Class: to provide a base template for child classes that have a common set of methods and properties, but the implementation can differ.
- Class: to create reusable objects with common properties and methods.
- Interface: to ensure consistency and interoperability between different classes, especially when working with large code bases.
Important Points
- Abstract classes cannot be instantiated.
- Abstract classes can have both abstract and non-abstract methods.
- Classes can only extend one class, but implement multiple interfaces.
- Interfaces can only contain method signatures, but no implementation.
- The implementer class must provide implementation for all the methods defined in the interface.
- Class constructors are called when an object is created, and they have the same name as the class.
- The
extends
keyword is used to inherit from a class, and theimplements
keyword is used to implement an interface.
Summary
In summary, PHP offers three ways to create classes: Abstract classes, Classes, and Interfaces. Abstract classes are used as the base template for the child classes with a common set of method and properties. Class is the basic template for creating objects, and interface is used as a contract between different classes to ensure consistency and interoperability. Understanding the differences and uses of these three types is essential in writing efficient and reusable code.