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);
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:
- Data and behaviour are separated. The student's data is in one place and the
functions that use it are scattered everywhere. Nothing physically connects
payFees()to students โ it just hopes you pass it the right kind of array. - Nothing is protected. Any line of code anywhere can do
$student1["balance"] = -999999;and corrupt your data. There are no rules. - Typos explode late. Write
$student["blance"]and PHP won't complain until that exact line runs โ maybe in front of your users. - Repetition everywhere. Need lecturers too? You'll copy-paste similar arrays and similar functions, and fixing a bug means fixing it in five places.
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:
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:
| Pillar | Plain meaning | Everyday example |
|---|---|---|
| Encapsulation | Keep data safe inside the object; interact only through approved doors | An ATM: you press buttons, you never reach into the cash box yourself |
| Inheritance | New classes can reuse and extend existing ones | A child inherits family traits, then adds their own |
| Polymorphism | Different objects respond to the same instruction in their own way | Tell a dog, a cat and a duck to "speak" โ you get a bark, a meow and a quack |
| Abstraction | Hide complicated details behind a simple surface | A 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.
What is the core idea of OOP?
An ATM lets you withdraw money through buttons but never lets you open the cash box. Which pillar is that?
- 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.