Internet
Fact-checked

At WiseGEEK, we're committed to delivering accurate, trustworthy information. Our expert-authored content is rigorously fact-checked and sourced from credible authorities. Discover how we uphold the highest standards in providing you with reliable knowledge.

Learn more...

What Is an Abstract Class in PHP?

Alex Newth
Alex Newth

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.

Woman doing a handstand with a computer
Woman doing a handstand with a computer

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.

Discuss this Article

Post your comments
Login:
Forgot password?
Register:
    • Woman doing a handstand with a computer
      Woman doing a handstand with a computer