What Is an Abstract Class in PHP?

Article Details
  • Written By: Alex Newth
  • Edited By: Angela B.
  • Last Modified Date: 22 September 2019
  • Copyright Protected:
    Conjecture Corporation
  • Print this Article
Free Widgets for your Site/Blog
The average American has around 60 "bad days" a year; lack of sleep is the biggest contributing factor.  more...

October 20 ,  1973 :  The "Saturday Night Massacre"  more...

Hypertext Preprocessor (PHP) is a scripting language that serves as an all-purpose language primarily employed on websites; it talks to databases, creates special effects and logic, and helps with administrative functions such as managing website members. The class code in PHP is used to make a capsule of coding that can be used continuously, so the programmer does not need to write the same coding for each instance. The abstract class in PHP, which was introduced with PHP5, is similar to the regular class but is used when only some of the class’s code will be needed, while other sections will need to be defined for the specific usage to work correctly. The abstract class in PHP can be set as public or protected, but not private.

The way a general class works in PHP is the programmer gives the class a title — such as “vehicle” if the coding is for a vehicle — that can be recalled later. Within this class, the programmer will write a set of instructions, such as code to make a vehicle turn left or brake. This type of coding emulates object-oriented coding, or coding that separates parts of the code into objects that have their own sets of code. PHP is not naturally an object-oriented code but, through this method, can emulate it.


An abstract class in PHP is built in a similar way. The main difference in the coding with an abstract class in PHP is that the class structure starts with “abstract class” instead of “class.” When this code is made, it creates an incomplete guide for the program to look at; the incomplete guide can then be filled in later.

The incomplete portions are filled in when the abstract class is called during the program. For example, the vehicle abstract class will have basic instructions that work with all types of vehicles, such as cars, trucks and motorcycles. Each vehicle, though, has its own specific needs, and the abstract class will allow the programmer to fill in these specific needs without having to write separate coding for each vehicle. This makes coding more time efficient, minimizes mistakes and makes it easier to make quick changes to every object that uses the abstract class.

PHP classes are specified as public, protected or private. Each of these labels acts as a shield that limits visibility of the code according to a user’s access level. Setting a class as public allows any user to see the code, protected status limits some of the code, and private allows only specified users see the code. The abstract class in PHP can be set as public or protected, but it cannot be private.


You might also Like


Discuss this Article

Post your comments

Post Anonymously


forgot password?