TExcelTAD
Home › PHP › PHP What is OOP

PHP What is OOP

Object-oriented programming (OOP) is a way of organising code around things rather than around steps. Instead of passing loose arrays and variables from function to function, you bundle data together with the functions that work on that data. The bundle is called an object, and the blueprint that describes it is called a class.

PHP has supported objects since PHP 5, and modern PHP (8.x) has a rich, strict object model: typed properties, constructor promotion, enums, readonly properties, interfaces, traits and more. Almost every popular PHP framework — Laravel, Symfony, WordPress plugins written today — is built from classes.

Procedural vs object-oriented

Here is a tiny shopping-cart calculation written in the procedural style. The data (an array) and the logic (functions) live separately:

php · try it yourself
<?php
function cartAdd(array $cart, string $item, float $price): array {
    $cart[] = ['item' => $item, 'price' => $price];
    return $cart;
}

function cartTotal(array $cart): float {
    $sum = 0;
    foreach ($cart as $line) {
        $sum += $line['price'];
    }
    return $sum;
}

$cart = [];
$cart = cartAdd($cart, 'Notebook', 3.5);
$cart = cartAdd($cart, 'Pen', 1.25);
echo "Items: " . count($cart) . "\n";
echo "Total: " . cartTotal($cart) . "\n";
Items: 2
Total: 4.75

It works, but nothing stops another part of the program from putting a bad value into $cart, and every function has to know the exact array layout. The same idea as a class:

php · try it yourself
<?php
class Cart {
    private array $lines = [];

    public function add(string $item, float $price): void {
        $this->lines[] = ['item' => $item, 'price' => $price];
    }

    public function count(): int {
        return count($this->lines);
    }

    public function total(): float {
        return array_sum(array_column($this->lines, 'price'));
    }
}

$cart = new Cart();
$cart->add('Notebook', 3.5);
$cart->add('Pen', 1.25);
echo "Items: " . $cart->count() . "\n";
echo "Total: " . $cart->total() . "\n";
Items: 2
Total: 4.75

The output is identical, but now the cart owns its data. Code outside the class can only use the public methods add(), count() and total(); the internal $lines array is hidden.

Key vocabulary

Term Meaning PHP example
Class A blueprint describing data and behaviour class Cart { ... }
Object / instance One concrete thing built from a class $cart = new Cart();
Property A variable that belongs to an object private array $lines
Method A function that belongs to a class public function total()
$this Inside a method, the current object $this->lines
-> Access a property or method of an object $cart->add(...)

The four pillars

OOP is usually described with four ideas. You will meet each one in the following lessons.

  1. Encapsulation — hide internal details and expose a small, safe interface (private / public).
  2. Inheritance — build a new class on top of an existing one (extends).
  3. Polymorphism — different classes can be used through the same interface, each responding in its own way.
  4. Abstraction — describe what something does without fixing how (abstract classes, interface).

A quick taste of polymorphism — two unrelated shapes that both answer the question "what is your area?":

php · try it yourself
<?php
class Square {
    public function __construct(private float $side) {}
    public function area(): float { return $this->side ** 2; }
}

class Circle {
    public function __construct(private float $r) {}
    public function area(): float { return round(M_PI * $this->r ** 2, 2); }
}

$shapes = [new Square(3), new Circle(1), new Square(1.5)];
foreach ($shapes as $shape) {
    echo get_class($shape) . ": " . $shape->area() . "\n";
}
Square: 9
Circle: 3.14
Square: 2.25

The loop does not care which class each object belongs to — it just calls area().

Tip: OOP is not "better" in every situation. A 20-line script is often clearer as plain functions. Classes pay off when data and rules grow and several parts of a program need to share them safely.

Why learn OOP in PHP?

  • Frameworks and libraries expect you to write and extend classes.
  • Type declarations on properties and methods catch mistakes early.
  • Composer autoloading (one class per file) keeps large projects organised.
  • Objects make testing easier: you can swap a real database class for a fake one.

Test yourself

What is the relationship between a class and an object?

  1. They are the same thing
  2. A class is a blueprint; an object is an instance built from it
  3. An object is a blueprint; a class is an instance
  4. A class is a kind of array

Which operator calls a method on an object in PHP?

  1. .
  2. ::
  3. ->
  4. =>

Hiding an object's internal data behind public methods is called…

  1. Inheritance
  2. Encapsulation
  3. Polymorphism
  4. Recursion