New tutorials View all
Encapsulation in PHP
Abstract classes in PHP
An introduction to MySQL databases in PHP
Creating a user login and registration system.
First Lesson of HTML
New forum replies
Confused in The big and general IRC Bot...Jim in Php irc bot logwescooldude3 in Php irc bot logJim in AJAX + loadingJim in Php irc bot say namewescooldude3 in Php irc bot say nameRealShadow in AJAX + loading
New frontpage replies
tutorials: Creating an IRC bot in PHPtutorials: Creating an IRC bot in PHPtutorials: Creating an IRC bot in PHPtutorials: Creating an IRC bot in PHPtutorials: Creating an IRC bot in PHP
New articles
New news
Abstract classes in PHP (1165 views)
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:
| php | |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php class Bike { // Main bike functions } class Kawasaki extends Bike { // Some function specially for Kawasaki bikes } ?> |
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:
| php | |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php class Bike { // Main bike functions } class Kawasaki extends Bike { public function getStockColor() { return "green"; } } ?> |
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.
| php | |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?php abstract class Bike { // Main bike functions // Add an abstract function abstract function getStockColor(); } class Kawasaki extends Bike { // The subclass } $myBike = new Kawasaki; ?> |
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:
| php | |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?php abstract class Bike { // Main bike functions // Add an abstract function abstract function getStockColor(); } class Kawasaki extends Bike { function getStockColor() { return "Green"; } } $myBike = new Kawasaki; echo $myBike->getStockColor(); ?> |
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:
| php | |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
Next time I will talk about interfaces, very similar to abstract classes but in the end also very different.
Replies on Abstract classes in PHP:
Jump to comment page: 1
There are no replies for this tutorial. Are you going to be the first?
Jump to comment page: 1
You are not logged in. Please login or register an account, it just takes 30 seconds.
