PHPOOP Introduction

OOP Introduction

Everything you have written so far in this course — variables, functions, arrays — is procedural code: a script that runs top to bottom, passing data between plain functions. Object-oriented programming (OOP) is a different way of organizing the same logic. It bundles data and the functions that operate on that data into a single unit called an object, built from a blueprint called a class. Once you start modeling real things — a product, a user, a shopping cart — OOP tends to keep the code closer to how you already think about the problem.

What OOP actually buys you

OOP is not a magic performance boost or a requirement for "real" code — plenty of good PHP is procedural. What it buys you is structure as a program grows: encapsulation keeps related data and behavior together instead of scattered across loose arrays and functions, reuse lets you extend or compose existing classes instead of duplicating logic, and modeling real entities as objects makes the code read closer to the domain it describes — a Product object behaves like a product, not like an anonymous chunk of array keys.

The same problem, two ways: procedural first

Suppose we need to represent a store product and calculate its price including tax. Here is a purely procedural approach: the product is just an associative array, and a standalone function knows how to compute its taxed price.

Procedural: a product as an array plus a helper function

PHP
<?php
function calculateTaxedPrice(array $product): float
{
    return round($product['price'] * (1 + $product['taxRate']), 2);
}

$product = [
    'name'    => 'Wireless Mouse',
    'price'   => 25.00,
    'taxRate' => 0.08,
];

echo $product['name'] . ': $' . calculateTaxedPrice($product) . "\n";
Wireless Mouse: $27

This works, but nothing ties the array to the function. Any part of the codebase can build a $product array with the wrong keys, misspell 'taxRate', or call calculateTaxedPrice() on something that isn't even a product — PHP has no way to catch that until it fails at runtime.

The same problem, rewritten with a class

Now the same behavior, modeled as a Product class. The data (name, price, taxRate) and the logic that operates on it (getTaxedPrice()) live together in one definition.

OOP: a Product class owns its data and behavior

PHP
<?php
class Product
{
    public string $name;
    public float $price;
    public float $taxRate;

    public function __construct(string $name, float $price, float $taxRate)
    {
        $this->name = $name;
        $this->price = $price;
        $this->taxRate = $taxRate;
    }

    public function getTaxedPrice(): float
    {
        return round($this->price * (1 + $this->taxRate), 2);
    }
}

$product = new Product('Wireless Mouse', 25.00, 0.08);

echo $product->name . ': $' . $product->getTaxedPrice() . "\n";
Wireless Mouse: $27

The output is identical, but the shape of the code changed. There is no way to accidentally create a Product missing its taxRate — the constructor requires it. getTaxedPrice() is not a free-floating helper that happens to accept a product-shaped array; it is part of what a Product is. We will build on this exact Product class across the next several pages (classes and objects, properties and methods, constructors, and visibility), so it is worth getting comfortable with this shape now.

A preview of the four pillars

Object-oriented programming is usually summarized as four related ideas. You do not need to master all of them yet — later pages in this course cover each in depth — but it helps to know the names and the one-sentence gist of each up front.

  • Encapsulation — bundling data and the methods that act on it inside one class, and controlling which parts are exposed to the outside world (covered in the Visibility page).

  • Inheritance — letting one class reuse and extend the properties and methods of another, so a DigitalProduct can build on everything Product already does instead of duplicating it.

  • Polymorphism — different classes responding to the same method call in their own way, so code that works with a Product can also work with any class that behaves like one.

  • Abstraction — hiding the messy implementation details behind a simple, well-named interface, so callers work with $product->getTaxedPrice() instead of re-deriving the tax formula themselves.

You already used a class without a class keyword
If you have ever written `$date = new DateTime()` or caught an exception with `catch (Exception $e)`, you were already using PHP's built-in classes. OOP is not a separate language bolted onto PHP — it is baked into the standard library you have been using all along.
Tip
Do not force every script into OOP just because it exists. A short one-off script that transforms some data is often clearer as plain functions. Reach for a class when you notice the same group of data traveling together through multiple functions — that is usually the signal that the data and its behavior belong in one place.