Lesson

PHP What is OOP

10 min reading
Free Course

What is OOP in PHP? Classes, Objects, and Core Principles

Object-Oriented Programming (OOP) organizes PHP code around objects that bundle related data (properties) and behavior (methods) into reusable blueprints called classes. While small scripts work fine with standalone functions, OOP keeps growing applications modular, predictable, and easy to test.

Procedural vs Object-Oriented PHP

In procedural code, arrays and helper functions float separately across files, making it easy for mismatched keys or invalid state to creep in. OOP groups state and validation inside a single object boundary.

flowchart LR
    C["Class Blueprint: Invoice"] --> O1["Object 1: Invoice #101 ($49)"]
    C --> O2["Object 2: Invoice #102 ($120)"]
    O1 --> M["Shared Methods: addItem(), markPaid()"]
    O2 --> M

The Four Pillars of OOP in PHP

Every major PHP framework—including Laravel and Symfony—relies on four practical OOP ideas:

  • Encapsulation: Keep internal state private or protected and expose safe methods that enforce business rules.
  • Abstraction: Hide complex implementation details behind clean method names or interface contracts.
  • Inheritance: Share common behavior from a parent class down to specialized child classes using extends.
  • Polymorphism: Allow different classes implementing the same interface to be swapped interchangeably by callers.
<?php
declare(strict_types=1);

class BankAccount
{
    public function __construct(
        private string $owner,
        private float $balance = 0.0
    ) {}

    public function deposit(float $amount): void
    {
        if ($amount <= 0) {
            throw new InvalidArgumentException('Deposit must be positive.');
        }
        $this->balance += $amount;
    }

    public function summary(): string
    {
        return "{$this->owner}: $" . number_format($this->balance, 2);
    }
}

$account = new BankAccount('Tariq', 100.0);
$account->deposit(45.50);
echo $account->summary() . PHP_EOL;

Common Mistakes to Avoid

  • Making every property public: Public properties allow external code to set invalid states like $account->balance = -999. Protect invariants behind methods.
  • Building giant "God classes": Keep each class focused on one clear responsibility instead of stuffing auth, billing, and mailing into a single User class.
  • Forgetting declare(strict_types=1);: Typed class properties and constructor parameters prevent subtle type mismatches across objects.

Frequently Asked Questions

Is procedural PHP completely bad?

No. Simple CLI scripts, config files, and tiny utilities are often cleaner as small functions. OOP shines as soon as your app manages domain models, services, and multiple collaborators.

What is a DTO (Data Transfer Object) in PHP?

A DTO is a lightweight class (often using PHP 8.2 readonly properties) whose only job is carrying typed structured data between layers like controllers and services.

How does Composer autoloading work with OOP classes?

Under PSR-4, placing class InvoiceService in app/Services/InvoiceService.php under namespace App\Services; lets Composer load the file automatically whenever you reference it.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground
PHP Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum