YogeshChauhan . com

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. 

amazon

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 How to Use SQL MAX() Function with Dates? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

Jun 16 What are Stored Procedures for SQL Server? Jun 16 What are Class Constants in PHP? Jun 15 A short basic guide on states in React Jun 15 How to define constants in PHP? Jun 15 How to define visibility for a property in PHP? Jun 15 How to use @if and @else in SCSS?

You might also like these

CSS Overflow Property with ExamplesCSSClearwater Seafoods – B2C in ChinaMiscCreate a responsive pricing table using simple HTML and CSSCSSWhat is Hadoop and Hadoop Ecosystem?MiscHow to remove WordPress TinyMCE Editor buttons?WordPressHow to center an image horizontally and vertically?CSS