PHP OOP Introduction

PHP What is OOP?

From PHP 5 onwards, you can write PHP code in an Object-Oriented Programming (OOP) style. Object-Oriented Programming is a programming paradigm that revolves around the concept of "objects", which contain both data and the functions that manipulate that data.

Before OOP, PHP was primarily written using Procedural Programming.


Procedural vs. Object-Oriented

Why Use OOP?

OOP is the industry standard for modern web applications because it provides several massive advantages:

  1. Faster Execution: OOP is generally faster and easier to execute in large-scale applications.
  2. Clear Structure: It provides a clear, highly organized structure for your programs.
  3. DRY Principle: It helps keep PHP code DRY ("Don't Repeat Yourself"), making the code easier to maintain, modify, and debug over time.
  4. Reusable Components: It allows you to create fully reusable applications with less code and a shorter development time.

What are Classes and Objects?

To understand OOP, you must understand the difference between a Class and an Object.

Think of a Class as a blueprint or a template, and an Object as an instance of that blueprint.

Example 1: Fruits

A "Fruit" has properties like color and weight. An "Apple" is an object created from the Fruit class, and we can define its specific color as Red and its weight as 150g.

Example 2: Cars

When the individual objects are created, they automatically inherit all the properties and behaviors from the class, but they can have different values assigned to their properties.


Exercise

?

In Object-Oriented Programming, what is considered the "blueprint" from which individual items are created?