Object-Oriented Programming in PHP

30 VIEWS

Programming today has become modular; which is separating or dividing a systems component that offers the benefit of flexibility and faster application development. Programmers are searching for ways to create a modular structure for their applications and one way to achieve this is through Object-Oriented Programming (OOP). The significance of this article lies in the fact that from the time OOP was introduced in PHP 3 till PHP 4, it was never a rich object model. However, with the development of PHP 5, OOP in PHP has advanced to incorporate a wider scope, enhanced performance and improved functionality.

Object-Oriented Programming is a style of programming that uses classes to group data and improve the structure of an application. This article will take you through Object-Oriented Programming in PHP by considering the main OOP concepts.

Object-Oriented Concepts

1. Class

Think of a Class as a container that groups functions, variables, and constants. Classes in OOP aid in organizing code. In declaring a class, you begin with the keyword “class” followed by the name of the class; lastly the class is enclosed in curly brackets ({}) as demonstrated below. The curly brackets enclose the properties and methods that belong to a class.

1.1 Adding properties to a class

Variables stored within a class is known as properties. Properties store values such as integers, strings, and Boolean. In declaring a variable using PHP 4, begin with the keyword “var” followed by $ and the name of the variable. However, “var” is no longer required to declare variables but is still acceptable with PHP 5. When using PHP 5, you can declare a property using access modifiers such as public, protected, or private, followed by the variable name.

2. Objects

2.1 Creating objects from classes

Once a class has been constructed, multiple objects can be created. An object is an instance of a class. The process of creating objects from a class is known as instantiation.

2.2 Getting the properties of an object

Multiple objects of a class can be created, and attributes can be assigned to each class. In order to obtain the property of an object, begin by stating the object name, followed by ->, then the property name.

2.3 Setting object properties

To set the preparation time to “15 minutes” and the spice to “curry” in the pizza object, the code implemented is as follows:

3. Methods

3.1 Adding methods to a class

Within a class, functions are referred to as methods. Functions are local to a class and are used to obtain data on objects. In constructing a method, you may begin with the ‘public’ keyword, followed by ‘function’ and then the function name followed by parentheses. The function name should begin with a lower-case letter.

3.2 Calling member function

After an object is created, you can call a member function to obtain the variables and methods of the class. Let’s consider an example.

4. Inheritance

Inheritance enables a class to inherit values from other classes. This allows the code to be reusable. Inheritance is declared using the “extends” keyword. Following is a simple example:

5. Encapsulation

Encapsulation is a means of protection for classes. It binds the data within the code by wrapping the data into a class. In Encapsulation, the variables of a class will be hidden from other classes and can be accessed only through the methods of the class. Consider the following example. The variables name and salary have been set to private, meaning they can only be accessed through the methods of the ‘Person’ class.

6. Abstraction

Abstraction means showing the needed/relevant information and hiding the rest.

There are a few things to note when it comes to abstraction:

  1. Abstract classes can be created by simply adding the ‘abstract’ keyword before class.
  2. Abstract methods can be created by adding the ‘abstract’ keyword before the method name.
  3. We cannot instantiate an abstract class.
  4. An abstract class can have fully implemented methods.
  5. When an abstract class is inherited by the child class, the abstract method of the parent class must be implemented in the child class.
  6. If the child is not implementing any abstract methods, then the child class must be declared abstract.

Following is a simple example of abstraction:

There you go. You’ve learned most of the key concepts in OOP, and hopefully now you’ll be comfortable applying these concepts in PHP.

Do you think you can beat this Sweet post?

If so, you may have what it takes to become a Sweetcode contributor... Learn More.

Frederick Plange is a computer science major at Ashesi University, one of the top universities in Ghana aiming to develop ethical and entrepreneurial leaders to transform the nation. Frederick is a focused and goal driven individual that is passionate about technology and computers


Discussion

Click on a tab to select how you'd like to leave your comment

Leave a Comment

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

Menu