W3docs

Understanding Object-Oriented Programming in PHP

Object-Oriented Programming (OOP) is a popular programming paradigm that focuses on the use of objects to design and develop applications. It is widely used for

Object-Oriented Programming (OOP) is a way of structuring code around objects — self-contained units that bundle together data (called properties) and the behavior that acts on that data (called methods). Instead of writing a long list of functions that pass data around, you model the things in your problem domain (a user, an order, a database connection) as objects and let them manage their own state.

PHP has supported OOP since PHP 5, and modern PHP frameworks such as Laravel and Symfony are built almost entirely on it. This chapter explains what OOP is, why it exists, and how the core ideas map onto PHP syntax. By the end you will know the difference between a class and an object and recognize the four pillars of OOP: encapsulation, inheritance, polymorphism, and abstraction.

Procedural vs. object-oriented PHP

Before classes, PHP code was typically procedural — a sequence of statements and functions operating on plain variables:

$carMake = "Ford";
$carModel = "Mustang";

function describeCar($make, $model) {
  return "The car is a $make $model.";
}

echo describeCar($carMake, $carModel);
// The car is a Ford Mustang.

This works, but the data ($carMake, $carModel) and the function that uses it are loosely connected. As a project grows, it gets hard to track which functions belong with which data. OOP solves this by packaging the data and behavior together into a single class.

Classes and objects

The two foundational concepts in OOP are the class and the object:

  • A class is a blueprint. It describes what properties and methods an object will have, but it is not a value you can use directly — like an architectural plan for a house.
  • An object is a concrete instance created from that blueprint, with real values filled in — like an actual house built from the plan.

In PHP, you declare a class with the class keyword and create an object from it with the new operator:

class Car {
  public $make;
  public $model;
}

$car = new Car();   // $car is an object (an instance of Car)
$car->make = "Ford";
$car->model = "Mustang";

echo $car->make;    // Ford

You can create as many independent objects from one class as you need, and each keeps its own copy of the properties. The arrow operator -> accesses a property or method on an object. Learn more in PHP Classes and Objects.

The four pillars of OOP

OOP is built on four core principles. PHP gives you a dedicated language feature for each one.

Encapsulation

Encapsulation means keeping an object's data and the methods that change it together, and controlling access from the outside. By marking properties private and exposing them only through methods, you protect an object from being put into an invalid state.

class BankAccount {
  private $balance = 0;

  public function deposit($amount) {
    if ($amount > 0) {
      $this->balance += $amount;
    }
  }

  public function getBalance() {
    return $this->balance;
  }
}

$account = new BankAccount();
$account->deposit(100);
echo $account->getBalance();   // 100

Here $balance cannot be set to a nonsensical value directly — every change goes through deposit(), which validates the input.

Inheritance

Inheritance lets one class reuse the properties and methods of another. A child class declared with extends automatically gains everything from its parent and can add to or override it. This avoids duplicating shared code. See PHP Inheritance for details.

class Vehicle {
  public function start() {
    return "Engine started.";
  }
}

class Car extends Vehicle {
  public function openTrunk() {
    return "Trunk opened.";
  }
}

$car = new Car();
echo $car->start();        // Engine started.  (inherited)
echo $car->openTrunk();    // Trunk opened.     (its own)

Polymorphism

Polymorphism ("many forms") means different classes can respond to the same method call in their own way. Code that calls makeSound() does not need to know whether it is holding a Dog or a Cat:

class Dog {
  public function makeSound() { return "Woof"; }
}

class Cat {
  public function makeSound() { return "Meow"; }
}

foreach ([new Dog(), new Cat()] as $animal) {
  echo $animal->makeSound() . "\n";
}
// Woof
// Meow

This is most powerful when combined with interfaces, which guarantee that every class provides the same set of methods.

Abstraction

Abstraction means hiding implementation details behind a simple, well-defined interface. Users of a class work with what it does, not how it does it. PHP supports this through abstract classes and interfaces, which define method signatures without (or with partial) implementation.

Putting it together: a complete example

The class below combines properties, a constructor that runs automatically when the object is created, and a method:

class Car {
  public $color;
  public $make;
  public $model;

  function __construct($color, $make, $model) {
    $this->color = $color;
    $this->make  = $make;
    $this->model = $model;
  }

  function getDescription() {
    return "The car is a " . $this->make . " " . $this->model . " in " . $this->color . " color.";
  }
}

$car = new Car("red", "Ford", "Mustang");
echo $car->getDescription();
// The car is a Ford Mustang in red color.

What happens here:

  • The class Car declares three public properties — color, make, and model.
  • __construct() is a special method PHP calls the moment new Car(...) runs. It copies the arguments into the object's properties using $this, which always refers to the current object.
  • getDescription() reads those properties and returns a sentence.
  • new Car("red", "Ford", "Mustang") builds the object; calling $car->getDescription() produces the output shown in the comment.

When should you use OOP?

OOP shines when an application has many related concepts with their own data and behavior, when you want to reuse code across a project, or when you work with a framework that expects it. For a tiny one-off script, plain procedural functions may be simpler. Most real-world PHP — APIs, web apps, libraries — leans on OOP because it keeps growing codebases organized and testable.

Conclusion

Object-oriented programming organizes code around objects that bundle data with behavior. You define a class as a blueprint and create objects from it with new. The four pillars — encapsulation, inheritance, polymorphism, and abstraction — give you tools to protect data, reuse code, and keep large applications maintainable.

To go deeper, continue with PHP Classes and Objects, then explore inheritance, interfaces, abstract classes, and traits.

Practice

Practice
What is the function of 'Object-Oriented Programming (OOP)' in PHP?
What is the function of 'Object-Oriented Programming (OOP)' in PHP?
Was this page helpful?