Signup/Sign In

Object Oriented Programming(OOP) in PHP

Ever since the creation of programming languages, computer scientists have made their efforts to develop several ways to program a computer. Eventually, as the more and more developed programming languages came into existence, the way people used to write programs also changed. The methodology, algorithms, conceptualization, they went on evolving over years. These days, Object Oriented Programming is a popular methodology of programming any application.

A particular programming language has to be designed in a very specific way so that a particular methodology can be followed. As far as PHP is concerned, it is an Object Oriented Programming language. In this section, we will see what are the features in PHP that supports or enables it to become an Object-oriented programming language. First of all, let's try to understand What is Object Oriented Programming? (or OOP) and it's concepts.


What is Object Oriented Programming?

Object Oriented programming is a programming style that is associated with the concept of Class, Objects and various other concepts revolving around these two, like Inheritance, Polymorphism, Abstraction, Encapsulation etc.

OOP is designed in such a way that one should focus on an object while programming and not the procedure. An object can be anything that we see around us. It can be a human (that has some properties like - name, address, DOB and so on), a chair (portrayed by size, material, cost etc), a school (depicted by place, student strength, results) etc.

Object oriented programming brings programming close to real life, as we are always dealing with an object, performing operations on it, using it's methods and variables etc.


Concepts of OOP

Basic OOPS concepts

In the video below, we have explained the basic concepts of Object Oriented Programming with help of a very easy to understand example. If you want to skip the video, everything is covered below as well.

Let us try to understand a little about all these, through a simple example. Human Beings are living forms, broadly categorized into two types, Male and Female. Right? Its true. Every Human being(Male or Female) has two legs, two hands, two eyes, one nose, one heart etc. There are body parts that are common for Male and Female, but then there are some specific body parts, present in a Male which are not present in a Female, and some body parts present in Female but not in Males.

All Human Beings walk, eat, see, talk, hear etc. Now again, both Male and Female, performs some common functions, but there are some specifics to both, which is not valid for the other. For example : A Female can give birth, while a Male cannot, so this is only for the Female.

Human Anatomy is interesting, isn't it? But let's see how all this is related to PHP and OOPS. Here we will try to explain all the OOPS concepts through this example and in the next tutorial, we will have the technical definitons for all this.


Class

Here we can take Human Being as a class. A class is a blueprint for any functional entity which defines its properties and its functions. Like Human Being, having body parts, and performing various actions.


Inheritance

Considering HumanBeing a class, which has properties like hands, legs, eyes etc, and functions like walk, talk, eat, see etc. Male and Female are also classes, but most of the properties and functions are included in HumanBeing, hence they can inherit everything from class HumanBeing using the concept of Inheritance.


Objects

My name is Abhishek, and I am an instance/object of class Male. When we say, Human Being, Male or Female, we just mean a kind, you, your friend, me we are the forms of these classes. We have a physical existence while a class is just a logical definition. We are the objects.


Abstraction

Abstraction means, showcasing only the required things to the outside world while hiding the details. Continuing our example, Human Being's can talk, walk, hear, eat, but the details are hidden from the outside world. We can take our skin as the Abstraction factor in our case, hiding the inside mechanism.


Encapsulation

This concept is a little tricky to explain with our example. Our Legs are binded to help us walk. Our hands, help us hold things. This binding of the properties to functions is called Encapsulation.


Polymorphism

Polymorphism is a concept, which allows us to redefine the way something works, by either changing how it is done or by changing the parts using which it is done. Both the ways have different terms for them.