PHP abstract classes
In this article, we will see what abstract classes are in PHP and then learn how to use them.
¿What is an abstract class?
The following is a small excerpt from the book FUNDAMENTOS DE PROGRAMACIÓN by Luis Joyanes Aguilar. I believe he describes it much better in his book than I could with my own words.
An abstract class is a class that has no objects; or more precisely, it is a class that cannot have objects as instances of the base class. An abstract class describes attributes and behaviors common to other classes and leaves some aspects of the class’s functioning to concrete subclasses
Imagine you want to create a general classification for all vehicles. A “vehicle” is a broad concept that encompasses everything from cars and motorcycles to boats and airplanes. However, you can’t build a “vehicle” itself because it is too general.
In this case, the “Vehicle” class would be an abstract class. It defines common characteristics that all vehicles share: they have wheels (or something similar), a motor, can move, etc. But it doesn’t specify how they move, what type of engine they have, or how many wheels they possess.
Let’s watch an example of abstract class:
Example of an abstract class<?php abstract class ClaseAbstracta { // Forzar la extensión de clase para definir este método abstract protected function saludo($nombre); // Método común public function adios() { echo "adios"; } } class OtraClase extends ClaseAbstracta { protected function saludo($nombre){ return "hola $nombre<br/>"; } public function saludar($nombre){ echo $this->saludo($nombre); } } $a = new OtraClase(); $a->saludar("Raul"); $a->adios();<?php abstract class ClaseAbstracta { // Forzar la extensión de clase para definir este método abstract protected function saludo($nombre); // Método común public function adios() { echo "adios"; } } class OtraClase extends ClaseAbstracta { protected function saludo($nombre){ return "hola $nombre<br/>"; } public function saludar($nombre){ echo $this->saludo($nombre); } } $a = new OtraClase(); $a->saludar("Raul"); $a->adios();
hola Raul adios
Classes defined as abstract cannot be instantiated.
All methods marked as abstract in the parent class must be declared in the child class.
¿Why we use abstract classes?
- hierarchy: Establish a hierarchy of classes, where more specific classes inherit the characteristics of the abstract class.
- Code re-use: Avoid code repetition by defining common characteristics in each concrete class.
- Polymorphism: Allow objects of different classes to be treated the same way, thanks to inheritance.
Differences between Interfaces and classes in PHP
Haven’t we seen something similar called an interface?
But similar is not the same. Although the syntax is similar, they behave very differently. While an interface allows us to tell a class which methods must be implemented, an abstract class is like a partially made class or a half-finished class.
If you want to see it in a certain way, you can see it as if the abstract class were a form with fields made and fields that you must fill in.
You can use an abstract class by implementing an interface.
The book mentioned is a Spanish book, so if you want a similar book you can search something like programming fundamentals, I don’t know if you could found a book so vast like the Spanish one all but this is what I could do for you here.