Originally published on CodingCush.
PHP OOP, short for object oriented programming, is a way of organising PHP code into small reusable pieces called classes and objects. Instead of one long list of instructions, you group related data and the functions that work on that data into one tidy box. In this guide you will learn what a class is, what an object is, how inheritance saves you from repeating code, and how visibility keeps your data safe, with small examples you can run yourself. All code was run on PHP 8.3.32.
What Is OOP in One Minute?
Think of a class as a blueprint and an object as the house built from it. The blueprint says a house has rooms, doors and windows. Each house built from the blueprint is an object: same plan, different address, different colour, different people inside. In PHP, a class is the blueprint and an object is the thing you actually use.
A class holds two kinds of things. Properties are the data, like a car brand or a bank balance. Methods are the functions that work on that data, like starting the engine or depositing money. The big win is reuse: write the class once, create as many objects as you need, and fix bugs in one place instead of ten. If PHP itself still feels new, our guide to what PHP is, explained simply covers the basics first.
Your First Class: A Simple Car
Here is a class called Car with two properties, brand and year, and one method, describe, that builds a sentence from them:
<?php
class Car {
public string $brand;
public int $year;
public function describe(): string {
return "This is a {$this->year} {$this->brand}.";
}
}
$myCar = new Car();
$myCar->brand = "Swift";
$myCar->year = 2024;
echo $myCar->describe() . "\n";
echo $myCar->brand . "\n";
Output:
This is a 2024 Swift.
Swift
Three things to notice. The keyword class starts the blueprint, and public means the property can be read and changed from outside the class. The word new builds the object, and $this inside a method means "this very object I am working on". The arrow -> reaches into the object to read a property or call a method. If you have built a login system before, our comparison of PHP sessions vs cookies shows another place where small objects keep user data tidy.
The Classes and Objects Basics page in the official PHP manual on php.net, showing a simple class definition.
The Constructor: Setting Things Up at Birth
Setting each property by hand after creating an object gets old fast, and it is easy to forget one. A constructor is a special method called __construct that runs automatically the moment an object is created. Give it the values the object needs, and it sets them up for you:
<?php
class Car {
public string $brand;
public int $year;
public function __construct(string $brand, int $year) {
$this->brand = $brand;
$this->year = $year;
}
public function describe(): string {
return "This is a {$this->year} {$this->brand}.";
}
}
$myCar = new Car("Swift", 2024);
echo $myCar->describe() . "\n";
Output:
This is a 2024 Swift.
The constructor takes the brand and year as arguments, so one line creates a fully ready object. This is the pattern you will see in almost every real PHP class: the constructor receives the data the object cannot live without, and the rest of the methods use it. The official PHP manual page on classes and objects basics shows the same pattern with more examples.
Inheritance: Reuse, Do Not Repeat
An electric car is still a car, just with a battery added. Inheritance lets a new class reuse everything from an old one and add its own extras. The child class uses the keyword extends, and parent:: calls the parent version of a method:
<?php
class ElectricCar extends Car {
public int $batteryKwh;
public function __construct(string $brand, int $year, int $batteryKwh) {
parent::__construct($brand, $year);
$this->batteryKwh = $batteryKwh;
}
public function describe(): string {
return parent::describe() . " It has a {$this->batteryKwh} kWh battery.";
}
}
$ev = new ElectricCar("Nexon EV", 2024, 40);
echo $ev->describe() . "\n";
Output:
This is a 2024 Nexon EV. It has a 40 kWh battery.
ElectricCar did not rewrite the brand, year or describe logic. It borrowed them from Car and only added the battery. That is the whole point of inheritance: write shared behaviour once, in the parent, and let children add what makes them different. Frameworks like Laravel and CodeIgniter are built on exactly this idea, which is why our Laravel vs CodeIgniter vs Core PHP comparison keeps coming back to classes.
Visibility: public, private and protected
Not every property should be open to the world. Visibility controls who can touch a property or method. public means anyone can use it. private means only the class itself can use it. protected means the class and its children can use it, but nothing outside.
A bank balance is the classic example. Nobody should set it directly, or they could give themselves a million. Instead, keep it private and offer methods to change it safely:
<?php
class BankAccount {
private float $balance = 0;
public function deposit(float $amount): void {
if ($amount > 0) {
$this->balance += $amount;
}
}
public function getBalance(): float {
return $this->balance;
}
}
$account = new BankAccount();
$account->deposit(500);
$account->deposit(250);
echo "Balance: " . $account->getBalance() . "\n";
Output:
Balance: 750
The balance can only grow through deposit, and deposit refuses negative amounts. The getter, getBalance, lets the outside world read the balance without ever touching it directly. This pattern, hiding data behind methods, is called encapsulation, and it is the reason OOP code survives contact with real users.
Procedural vs OOP: Which One to Use?
PHP lets you write plain functions, and for a tiny script that is often the right call. OOP pays off as a project grows. This table sums up the trade off:
| Situation | Procedural style | OOP style |
|---|---|---|
| A 30 line contact form | Simple and fast to write | Overkill, adds files for no gain |
| A login system used on 20 pages | Functions copied and drifting apart | One User class, reused everywhere |
| A team project | Easy to break each other's code | Private properties protect shared data |
| Adding a new feature | Search and edit many places | Extend a class, change one place |
The honest rule is simple: start procedural for small scripts, switch to classes when you catch yourself copying the same code a second time. The PHP manual section on classes and objects is the reference to keep open while you practise.
Common Mistakes and Troubleshooting
Forgetting the dollar sign on $this. Inside a method, the object is always $this, with a dollar sign. Writing this->brand gives a parse error.
Using -> on the class instead of the object. Car->describe() fails, because describe belongs to an object, not to the blueprint. Create the object first with new Car().
Calling a private property from outside. $account->balance throws a fatal error when balance is private. That is the feature working, not a bug: use the getter method instead.
Forgetting parent::__construct in a child. If the child defines its own constructor and never calls the parent one, the parent properties stay empty. Call parent::__construct(...) first, then set the child extras.
Making everything public. It works at first, then any part of the code can change any property, and bugs become impossible to trace. Start private, open up only what the outside world truly needs.
Frequently Asked Questions
Do I need OOP to write PHP?
No. Small scripts work fine with plain functions. OOP becomes worth it when a project grows, when code gets reused, or when you work in a team.
What is the difference between a class and an object?
A class is the blueprint, the definition of what something has and does. An object is one thing built from that blueprint, with its own values, like one specific car from the Car class.
What does $this mean in PHP?
Inside a method, $this refers to the current object, the one the method was called on. It lets the method read and change that object's own properties.
Is OOP slower than procedural PHP?
The difference is tiny and does not matter for normal websites. Clean, reusable code saves far more time than the microseconds OOP costs.
Should every PHP file use classes?
No. A short script that does one job is fine as plain code. Reach for a class when the same idea appears in more than one place, or when data needs protecting.
Conclusion
PHP OOP is four ideas in a trench coat: a class is a blueprint, an object is the thing built from it, inheritance reuses what already works, and visibility protects what should not be touched. Start with one small class, give it a constructor, and let it grow only when it needs to. Your future self, debugging at 2 a.m., will be grateful.

Top comments (0)