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 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to uninstall Cocoapods from the Mac OS?

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
RIGHT JOIN in PostgresPostgresHow to set opacity or transparency using CSS?CSSHow to scroll contents of a an element vertically using JavaScript?JavaScriptIntroduction to components and templates Part 2: Templates and viewsAngularHow to check if the page is the home page in WordPress?WordPressHow to create two segues with two UIButtons on a single page (Swift 5.0)?Swift