A brief introduction to PHP

A server-side scripting language, PHP is used in web page development. In recent years, this language has become very popular due to its simplicity. The code can be combined with HTML scripts, and once the PHP code is executed, the web server sends the resulting content in the form of images or HTML documents that can be easily interpreted by the browser. The importance and capabilities of Object Oriented Programming or OOP are not unknown to PHP programmers and it is a widely used technique in all modern programming languages.

Popular OOP concepts implemented in PHP

Now let’s take a look at some of the commonly used concepts of Object Oriented Programming that find an implementation in the popular web development programming language that is PHP.

Inheritance

Inheritance is one of the most important object-oriented programming concepts that finds use in most of the commonly available programming languages. It is based on the principle of classes of children and parents. PHP allows inheritance to be achieved by extending 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

As in any object-oriented language, PHP includes the concepts of various access modifiers, namely Public, Private, and Protected.

  • Audience- Public access allows the code to be visible and can be modified by code from anywhere else.
  • Private- Private access makes the code visible, but it can only be modified from within the user class.
  • Protected- With the protected access modifier, the code remains visible but can be modified from within the class itself or from any of the corresponding child classes.

Overload

The overload function allows you to use two methods with the same name but different parameters. Let’s consider the following code snippet:

class Accountant extends Employee

{

civil service processSalarioMonthly ($eCode)

{

// code that processes the salary

}

public function processMonthlySalary ($eCode, $variablePayFlag, $variablePercentage)

{

if ($variablePayFlag)

echo “Please process salary with variable payment”;

the rest

echo “Please process salary without variable payment”;

}

}

In the code shown above, there are two methods that have the same name (‘processMonthlySalary’). When considering an organization, not all employees will have the variable pay component included in their salary. Therefore, the accountant will have to process the salary according to the two different approaches mentioned in the code: with variable pay and without variable pay. When the salary is processed with variable pay, the accountant must mention the amount involved in the variable pay component.

Mutators and accessories

Commonly known as setters and getters, Mutators and Accessors find wide application in all programming languages. Getters or accessors are functions that are used to return or get the value of variables at the class level. Setters or mutators are functions that are used to set or modify the value of any class level variable. These class types are used to collectively transfer data from one layer to another.

Static

The use of the static keyword for methods and variables is one of the most common practices involved in object-oriented programming. Static indicates that the method or variable is available to every instance of the class. Static variables or methods are used without creating an instance of a class. In fact, static variables cannot be accessed through the instance of any class. When creating a static method, care must be taken to ensure that the $this variable does not appear inside a static method.

abstract class

The abstract class is a very frequently used feature of PHP-based object-oriented programming. Abstract classes are those that cannot be instantiated, but are inherited. A class that inherits an abstract class may itself be another abstract class. Creating an abstract class in PHP is possible simply by using the ‘abstract’ keyword.

Interface

As far as object-oriented programming is concerned, the interface serves as a collection of definitions for a certain set of methods in a class. When an interface is implemented, the class is forced to follow the methods defined in it. PHP defines the interface like any other language by first defining it using the “interface” keyword. For the implementation, the same keyword must be mentioned in the implementation class. Let’s take a look at some sample code for a better understanding.

interface myIface

{

public function myFunction ($name);

}

class myImpl implements myIface

{

public function myFunction ($name)

{

// function body

}

}

conclusion

As far as PHP developers are concerned, this language has become one of the most popular choices as a development language for websites and web-based applications. It has undergone numerous improvements to support various aspects of programming. Of all these, understanding and implementing object-oriented functions is the most important.

Leave a Reply

Your email address will not be published. Required fields are marked *