<?php OOP: Zero to Hero Chapter 03 of 20

Chapter 03 ยท OOP Foundations

Why OOP? The Big Idea

Before learning how to write object-oriented code, you deserve to know why it exists. This chapter shows the pain of the old way, then reveals the beautiful idea that fixes it. After this, every chapter will feel like it has a purpose.

The old way: procedural code

So far, our programs have been a list of instructions and functions, top to bottom. That style is called procedural programming. It works fine for small scripts. But watch what happens when a program grows.

Imagine we're building a small system for a school. We need to handle students. The procedural way uses arrays and loose functions:

<?php
// Data lives here...
$student1 = ["name" => "Chanda", "program" => "BSc Computing", "balance" => 5000];
$student2 = ["name" => "Mutinta", "program" => "LLB", "balance" => 7200];

// ...and the functions that work on it live somewhere else
function payFees(array $student, float $amount): array {
    $student["balance"] = $student["balance"] - $amount;
    return $student;
}

function describe(array $student): string {
    return $student["name"] . " (" . $student["program"] . ") owes K" . $student["balance"];
}

$student1 = payFees($student1, 1500);
echo describe($student1);
Chanda (BSc Computing) owes K3500

It works! So what's the problem? Let the program grow to the size of a real school system โ€” hundreds of functions, thousands of lines โ€” and cracks appear:

๐Ÿงบ
Real-world analogy

Procedural code is like keeping all your belongings in one giant heap in the middle of the room: clothes, books, phone chargers, food. You can find things... at first. But as the heap grows, life becomes chaos. OOP is like getting a wardrobe, a bookshelf and a kitchen cupboard โ€” everything related lives together, behind its own door.

The big idea of OOP

Object-Oriented Programming is one simple, powerful idea:

The whole of OOP in one sentence

Bundle data and the functions that work on that data together into one unit โ€” an object โ€” and protect it with rules.

Instead of a loose array plus loose functions, we describe a Student as one self-contained thing that knows its own name and balance (data) and knows how to pay fees and describe itself (behaviour). Here's a sneak preview โ€” don't worry about the syntax yet, next chapter teaches every symbol:

class Student {
    public string $name;
    public string $program;
    public float $balance;

    public function payFees(float $amount): void {
        $this->balance -= $amount;
    }

    public function describe(): string {
        return "$this->name ($this->program) owes K$this->balance";
    }
}

Read it like English: "A Student has a name, a program and a balance. A Student can pay fees and describe itself." The code now mirrors the real world โ€” and that is why OOP took over the software industry.

The four pillars (a gentle preview)

You'll hear these four fancy words a lot. Here they are in plain language โ€” each one gets its own chapter later, so just wave hello for now:

PillarPlain meaningEveryday example
EncapsulationKeep data safe inside the object; interact only through approved doorsAn ATM: you press buttons, you never reach into the cash box yourself
InheritanceNew classes can reuse and extend existing onesA child inherits family traits, then adds their own
PolymorphismDifferent objects respond to the same instruction in their own wayTell a dog, a cat and a duck to "speak" โ€” you get a bark, a meow and a quack
AbstractionHide complicated details behind a simple surfaceA car's steering wheel: you turn it, you don't think about the mechanics underneath

Where you've already used OOP without knowing

If you've ever used WordPress, Laravel, Moodle or written new DateTime() โ€” you were using objects other people built. Every modern PHP framework, every plugin system, every serious application is object-oriented. Learning OOP is not optional for a PHP developer; it's the doorway to the professional world.

Quick quiz

What is the core idea of OOP?

Quick quiz

An ATM lets you withdraw money through buttons but never lets you open the cash box. Which pillar is that?

๐Ÿ”‘ Key points
  • Procedural code = loose data + loose functions. Fine for small scripts, chaotic at scale.
  • OOP bundles data + behaviour into one unit called an object.
  • Objects protect their data with rules (encapsulation) โ€” no more accidental corruption.
  • The four pillars: encapsulation, inheritance, polymorphism, abstraction.
  • All modern PHP (Laravel, WordPress internals, Moodle) is object-oriented.