OOP in PHP – Classes, Objects & Inheritance
In this lesson: Learn OOP in PHP — classes, objects, constructors, access modifiers and inheritance — with simple examples and output.
What is OOP
Object-Oriented Programming (OOP) is a way of organising code around objects — self-contained units that hold both data (properties) and actions (methods). Instead of scattered variables and functions, you group related things into a class. This keeps big projects tidy, reusable and easier to grow.
Class and object
<?php
class Car {
public $color = "red";
}
$myCar = new Car(); // create an object
echo $myCar->color; // red
?>
class Car is the blueprint; new Car() builds a real object. You reach an object's property with the arrow ->.
Properties and methods
Properties are the object's data; methods are its actions.
<?php
class Car {
public $brand = "Tata";
public function honk() {
return "Beep from " . $this->brand;
}
}
$c = new Car();
echo $c->honk();
?>
Inside a method, $this means "this object", so $this->brand reads the current car's brand.
The constructor
__construct() runs automatically when the object is created — perfect for setup.
<?php
class User {
public $name;
public function __construct($name) {
$this->name = $name;
}
}
$u = new User("Aman");
echo $u->name;
?>
Passing "Aman" to new User("Aman") sends it to the constructor, which stores it. No separate setup call needed.
Inheritance
A child class extends a parent and reuses its code.
<?php
class Animal {
public function eat() { return "eating"; }
}
class Dog extends Animal {
public function bark() { return "woof"; }
}
$d = new Dog();
echo $d->eat(); // inherited
echo $d->bark(); // its own
?>
Dog automatically has eat() from Animal, plus its own bark(). This avoids rewriting shared code.
public, private, protected
| Keyword | Reachable from |
|---|---|
| public | Anywhere |
| protected | The class and its children |
| private | Only inside the same class |
Make internal details private so outside code cannot change them by accident. Expose only what is needed as public. This idea is called encapsulation.
Common mistakes
- Forgetting
new. A class is only a blueprint; you neednewto make an object. - Using
.instead of->. Object members use the arrow->, not a dot. - Forgetting
$thisinside methods. Use$this->propto reach the object's own property. - Making everything public. Keep internal data private for safer, cleaner code.
Summary
- OOP groups data and actions into classes and objects.
- A class is a blueprint;
newbuilds an object; reach members with->. __construct()sets up an object automatically.extendsgives inheritance — reuse a parent's code.public/private/protectedcontrol access (encapsulation).
Next, deepen OOP with abstract classes vs interfaces, or reuse code with PHP traits.
Frequently Asked Questions
What is the difference between a class and an object?
Car is the class; your specific red car is an object of it. One class can make many objects.What does the constructor do?
__construct() method runs automatically when you create an object with new. It is used to set up the object's starting values, like giving a new user their name straight away.What is inheritance in OOP?
extends a parent and gets all its properties and methods, then can add or change its own. It avoids repeating shared code.What is the difference between public, private and protected?
public is reachable from anywhere, private only inside the same class, and protected inside the class and its child classes. Use private for internal details.What does $this mean in PHP?
$this refers to the current object — the specific one the method is running on. You use it to read or set that object's own properties, like $this->name.