January 24, 2020

PHP Object-Oriented Programming (OOPs) Beginner’s Tutorial

What is Object Oriented Programming

Object-Oriented Programming (OOP) is a programming model that is based on the concept of classes and objects. As opposed to procedural programming where the focus is on writing procedures or functions that perform operations on the data, in object-oriented programming the focus is on the creations of objects which contain both data and functions together.

Object-Oriented Programming works on these terms class, object, properties,  methods and Inheritance. To understand all the terms and oops concept we will take a example of robots.

Suppose a robots manufacturing company want to built a new robot. So they will start with a blueprint on paper which will have the following things mentioned on it.

  1. characteristic of that robots (Number of camera, sensor, arms and wheels)
  2. List of Functions that robots perform and how they will perform with help of what resource ( camera, sensor, arms and wheels )
Using this blueprint company can built as many robots as they want with same  characteristic and function.

After some time if this company want to improve this robot, they won’t have to work from scratch. They will use old blueprint and will add more functions or characteristic to improve that robot with new model name.

Now relate this example with oops concept : 
Robots blueprint is class, characteristic of that robots is class properties, list of function that robots perform and how they will perform is method of class. Robot built using the blueprint is object of that class. And when we use the old blueprint to built a advance robot that is called Inheritance. 

Advantages of OOPs

  1. Real world programming
  2. Easier to maintain
  3. Reusable of code
  4. Information hiding
  5. Programmers are able to reach their goals faster.

Fundamental OOP concepts

  1. Inheritance – Inheritance is an OOPS concept in which one object acquires the properties and behaviors of the parent object. It’s creating a parent-child relationship between two classes.
  2. Polymorphism – Polymorphism is the ability to process objects differently depending on their data type or class. More specifically, it is the ability to redefine methods for derived classes. For example, given a base class shape, polymorphism enables the programmer to define different area methods for any number of derived classes, such as circles, rectangles and triangles. No matter what shape an object is, applying the area method to it will return the correct results. 
  3. Abstraction – Abstraction means, showcasing only the required things to the outside world while hiding the details. Its main goal is to handle complexity by hiding unnecessary details from the user. That enables the user to implement more complex logic on top of the provided abstraction without understanding or even thinking about all the hidden complexity. Consider a real life example of a man driving a car. The man only knows that pressing the accelerators will increase the speed of car or applying brakes will stop the car but he does not know about how on pressing accelerator the speed is actually increasing, he does not know about the inner mechanism of the car or the implementation of accelerator, brakes etc in the car. This is what abstraction is.
  4. Encapsulation – The encapsulation is the process of grouping or wrapping up of data and functions to perform actions on the data into the single unit. The single unit is called a class. Encapsulation is like enclosing in a capsule. That is enclosing the related operations and data related to an object into that object. It keeps the data and the code safe from external interference. The main purpose or use of the encapsulation is to provide the security to the data of a class. To make the data secure we need to use private access modifiers that will restrict the access to the data outside the class. In this OOPS concept, the variables of a class are always hidden from other classes. It can only be accessed using the methods of their current class. Consider a real life example of encapsulation, in a company there are different sections like the accounts section, finance section, sales section etc. The finance section handles all the financial transactions and keep records of all the data related to finance. Similarly the sales section handles all the sales related activities and keep records of all the sales. Now there may arise a situation when for some reason an official from finance section needs all the data about sales in a particular month. In this case, he is not allowed to directly access the data of sales section. He will first have to contact some other officer in the sales section and then request him to give the particular data. This is what encapsulation is. Here the data of sales section and the employees that can manipulate them are wrapped under a single name “sales section”.

Leave a Reply