Yogesh Chauhan's Blog

A Quick Guide to Object-Oriented Programming in PHP

in PHP on February 7, 2020

What is OOP?

OOP = Object-Oriented Programming. It's an approach that models real world objects such as Cars, Fruits, Humans, Animals, Jobs and many more real world objects.

There are types of programming as well Procedural and Object Oriented.

The Procedural programming, as per its name, is about writing procedures that is going to perform some operations on data. But OOP is a bit more advanced. Its about creating objects that contain data as well as functions.

OOP has many advantages as well. For example, it provides a clear modular structure. Also, once you create objects, you can always reuse it. So, less code and less time to develop the applications as well as it makes less code to maintain or edit for bugs. Overall, its faster, better and easier approach and that's the reason many IT people love this approach, whether its PHP or Java.

OOP programming is faster and easier to execute as well. 

PHP OOP was added into PHP 5. Since then it has really helped to build really complex websites.

These are few concepts in OOP PHP that we need to keep in mind.

1. Class: Class is like a template. We can use that template to create many objects. For example, let's say we created a class named Car. Now, there are many different brands and models of a car and each one has it's unique functionality and features but at the end they are all cars with 4 tires, 4 or 2 doors, engine, battery, etc.

2. Object: Object is an instance of a class. When we create objects from a class, they inherit all the properties and behaviors from the class. You can add more properties to those objects as well. So, if you create Tesla model 3 object from a Car class then it will have all the properties of a Car and you can add more properties/features to that object to differentiate from other Car objects. Just like real world.

3. Inheritance: We must have heard this word like a millions of time. It simply means inheriting properties from parent class. As I just mentioned, when you create an object from a class, it will have same properties of a class unless you add more properties to it.

4. Polymorphism: When we use same function for different purposes, it's called Polymorphism. For example, we have one function named make_coffee, which will take coffee as an argument but the coffee might be different. Now, we have different methods in that function which will be executed for different types of coffee. So, if you pass an argument, regular coffee then it will execute regular coffee method and if you pass decaf argument then it will execute decaf coffee method.

5. Data Abstraction: You can hide implementation details but can show data and that is called Data Abstraction. 


Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
How does Binding work in JavaScript?JavaScriptCreate a simple bar chart and column chart using pure CSS gridCSS5 Ways to Loop Through JavaScript ArraysJavaScriptControl rendering using CSS content-visibility propertyCSSHow to apply style to the first-child using jQuery?jQueryHow to convert a function component into a class in React?React