A brief introduction to PHP
A server-side scripting language, PHP is used in the development of web pages. In recent years, this language has become largely popular because of its simplicity. The code can be combined with HTML scripts and once the PHP code gets executed, the web server sends forth the resultant content in the form of images or HTML documents that can be interpreted easily by the browser. The importance and capabilities of Object Oriented Programming or OOP is not unknown to the PHP programmers and is a technique widely used in all modern programming languages.
Popular OOP concepts implemented in PHP
Let us now take a look at some of the commonly used concepts of Object Oriented Programming that find an implementation in the popular web development scripting language that is PHP.
Inheritance is one of the most important OOP concepts that finds use in most of the commonly available programming languages. It is based on the principle of child and parent classes. PHP allows inheritance to be achieved by the extension of a class. When this is done, the child class inherits the properties of the parent class and also allows the addition of a number of properties.
Public, Private, and Protected
Just like in any object-oriented language, PHP includes the concepts of various access modifiers, namely Public, Private and Protected.
- Public- Public access allows the code to be visible and can be modified by a code from any place else.
- Private- Private access makes the code visible but can be modified only from within the user class.
- Protected- With the protected access modifier, the code stays visible but can be modified from either the same class or any of the corresponding child class.
The overloading feature enables two methods to be used with the same name but with different parameters. Let us consider the following snippet of code:
class Accountant extends Employee
public function processMonthlySalary ($ eCode)
// code that processes salary
public function processMonthlySalary ($ eCode, $ variablePayFlag, $ variablePercentage)
if ($ variablePayFlag)
echo "Please process the salary with variable pay";
echo "Please process the salary without variable pay";
In the code shown above, there are two methods bearing the same name ('processMonthlySalary'). When an organization is taken into account, not every employee will have the variable pay component included in their salary. Thus the accountant will have to process the salary as per the two different approaches mentioned in the code- with variable pay and without variable pay. When the salary is processed using variable pay, the accountant has to mention the amount involved in the variable pay component.
Mutators and Accessors
Commonly referred to as setters and getters, the Mutators and Accessors find wide application in all programming languages. Getters or accessors are functions used to return or get the value of variables at the class level. Setters or mutators are functions used for setting or modifying the value of any class level variable. These class types are used for transferring data in a collective manner from one layer to another.
The use of the static keyword for methods as well as variables is one of the most common practices involved in object-oriented programming. Static indicates that the method or variable is available for every instance of the class. Static variables or methods are used without an instance of a class being created. In fact, static variables cannot be accessed via the instance of any class. When a static method is being created, care should be taken to ensure that the $ this variable does not get featured within a static method.
Abstract class is one feature of PHP-based object oriented programming that is used very frequently. Abstract classes are those that cannot be instantiated, rather they are inherited. A class inheriting an abstract class can be itself be another abstract class. Creation of an abstract class in PHP is possible simply by use of the 'abstract' keyword.
As far as object-oriented programming is concerned, the interface serves as a collection of definitions of a certain set of methods in a class. When an interface is implemented, the class is forced to follow the methods defined in the same. PHP defines interface just as in any other language by first defining it using the "interface" keyword. For implementing, the very same keyword needs to be mentioned in the implementing class. Let us take a look at a sample code for better understanding.
public function myFunction ($ name);
class myImpl implements myIface
public function myFunction ($ name)
// function body
As far as the PHP programmers are concerned, this language has emerged as one of the most popular options as a website and web-based application development language. It has undergone numerous enhancements for supporting various aspects of programming. Of all these, understanding and implementing the object-oriented features ranks as the most important.