Abstract classes in PHP
An abstract class is always a parent class that serves as a sort of blueprint for the subclasses it might hold. This advanced PHP OOP subject will be explained in this tutorial.
Before reading this article I assume you know basic OOP, including inheritance and encapsulation.
Also, please note that abstract classes (in way way a form of encapsulation) is only possible since PHP5.
This article is about Abstract classes. You’ve read a small introduction in the description, although some people won’t agree with me saying it’s always a parent class. You will figure out why in just a few minutes.
When defining a new class with a subclass it’s simple and very loose. You can do whatever you want in the subclass, without many restrictions from above. With an abstract class you can define some rules for the subclass.
Lets say we’ll have the following setup:
Now visualise we would add the following subclasses: Honda, Ducati and MV Agusta. That will leave us with 4 subclasses. We can now fill each subclass with all desired function that’s specific to each bike.
But something happens, we need to know the (factory) favorite colour of all biks, we can do that by making functions for all subclasses:
That will work perfect for all subclasses. But imagine you working on these classes quite much and you have a huge system. A system where you cannot remember if you have the getStockColor function in each bike subclass.
Lets say you forgot to implement the function in the Ducati subclass and this has the result that your whole application is screwed, all Ducati pages parse with errors.
Of course this is a lame example, but this little fault is made quite often.
This is where the Abstract class kicks in. In the abstract class we can make sure all our subclasses have the methods we want. Take a look at the following example of our Bike class.
I’ve turned the Bike class into an abstract class and added the getStockColor function as an abstract function (method) to the class.
When defining a abstract function in an abstract class, all subclasses of the parent class have to implement the abstract function. Or else it will result in a fatal error:
Fatal error: Class Kawasaki contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Bike::getStockColor) in /var/vhost/jim.zonax.net/index.php on line 17
Now add the function to the subclass:
This will output:
Green
So using the abstract classes will make sure you have the required functions in all subclasses, you can just expect all subclasses have that function because it won’t even parse if that would not be the case.
And don’t worry about your basic functions in your main class. As long as they are not abstract they can function as proper functions:
Next time I will talk about interfaces, very similar to abstract classes but in the end also very different.
Also, please note that abstract classes (in way way a form of encapsulation) is only possible since PHP5.
This article is about Abstract classes. You’ve read a small introduction in the description, although some people won’t agree with me saying it’s always a parent class. You will figure out why in just a few minutes.
When defining a new class with a subclass it’s simple and very loose. You can do whatever you want in the subclass, without many restrictions from above. With an abstract class you can define some rules for the subclass.
Lets say we’ll have the following setup:
Code:
1
<?php2
3
class Bike {4
5
// Main bike functions6
7
}8
9
class Kawasaki extends Bike {10
11
// Some function specially for Kawasaki bikes12
13
}14
15
?>Now visualise we would add the following subclasses: Honda, Ducati and MV Agusta. That will leave us with 4 subclasses. We can now fill each subclass with all desired function that’s specific to each bike.
But something happens, we need to know the (factory) favorite colour of all biks, we can do that by making functions for all subclasses:
Code:
1
<?php2
3
class Bike {4
5
// Main bike functions6
7
}8
9
class Kawasaki extends Bike {10
11
public function getStockColor() {12
return “green”;13
}14
15
}16
17
?>That will work perfect for all subclasses. But imagine you working on these classes quite much and you have a huge system. A system where you cannot remember if you have the getStockColor function in each bike subclass.
Lets say you forgot to implement the function in the Ducati subclass and this has the result that your whole application is screwed, all Ducati pages parse with errors.
Of course this is a lame example, but this little fault is made quite often.
This is where the Abstract class kicks in. In the abstract class we can make sure all our subclasses have the methods we want. Take a look at the following example of our Bike class.
Code:
1
<?php2
3
abstract class Bike {4
5
// Main bike functions6
7
8
// Add an abstract function9
abstract function getStockColor();10
11
}12
13
class Kawasaki extends Bike {14
15
// The subclass16
17
}18
19
$myBike = new Kawasaki;20
21
?>I’ve turned the Bike class into an abstract class and added the getStockColor function as an abstract function (method) to the class.
When defining a abstract function in an abstract class, all subclasses of the parent class have to implement the abstract function. Or else it will result in a fatal error:
Fatal error: Class Kawasaki contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Bike::getStockColor) in /var/vhost/jim.zonax.net/index.php on line 17
Now add the function to the subclass:
Code:
1
<?php2
3
abstract class Bike {4
5
// Main bike functions6
7
8
// Add an abstract function9
abstract function getStockColor();10
11
}12
13
class Kawasaki extends Bike {14
15
function getStockColor() {16
return "Green";17
}18
19
}20
21
$myBike = new Kawasaki;22
echo $myBike->getStockColor();23
24
?>This will output:
Green
So using the abstract classes will make sure you have the required functions in all subclasses, you can just expect all subclasses have that function because it won’t even parse if that would not be the case.
And don’t worry about your basic functions in your main class. As long as they are not abstract they can function as proper functions:
Code:
1
<?php2
3
abstract class Bike {4
5
public function __construct() {6
echo "wroem";7
}8
9
abstract function getStockColor();10
11
}12
13
class Kawasaki extends Bike {14
15
function getStockColor() {16
return "Green";17
}18
19
}20
21
$myBike = new Kawasaki;22
echo $myBike->getStockColor();23
24
?>Next time I will talk about interfaces, very similar to abstract classes but in the end also very different.
No replies
You are not logged in. To reply to this tutorial, please login. If you dont have an account yet, you can register here.






