Skip to content
AgileLogics
6 min read

Object-Oriented Programming in Depth

Prerequisites: Level 1 — Beginner

PHP's OOP model supports inheritance, interfaces, and abstract classes — the same core ideas covered for Java and C#, expressed in PHP's own syntax.

abstract class Shape
{
    abstract public function area(): float;

    public function describe(): string
    {
        return "Area: " . $this->area();
    }
}

interface Printable
{
    public function toString(): string;
}

class Circle extends Shape implements Printable
{
    public function __construct(private float $radius) {}

    public function area(): float
    {
        return M_PI * $this->radius ** 2;
    }

    public function toString(): string
    {
        return "Circle(r={$this->radius})";
    }
}

private/protected/public work exactly like Java's access modifiers; extends allows single inheritance from one class, while implements allows a class to satisfy any number of interfaces.

Common mistakes

  • Forgetting a class implementing an interface must implement every method that interface declares — a missing one is a fatal error, not a warning.