Kritim Yantra
Sep 06, 2025
Let’s be honest—when most beginners start with PHP, they write everything in one file. Functions here, variables there, HTML mixed in… and soon, it becomes a big mess. Sound familiar? 😅
That’s where OOP (Object-Oriented Programming) comes to the rescue.
OOP in PHP makes your code more organized, reusable, and easier to manage. Instead of writing the same code again and again, you create blueprints (called classes) and use them to build objects. Think of it like designing a house plan once and building as many houses as you want with it.
OOP is a way of writing code that revolves around objects.
👉 Imagine you’re creating a website for managing employees. Instead of writing separate functions for each employee, you create a class Employee and use it to generate multiple employees with different data. Neat, right?
A class is a template, and an object is an instance of that class.
<?php
class Car {
public $brand;
public $color;
public function start() {
echo "The car has started 🚗";
}
}
// Create an object
$myCar = new Car();
$myCar->brand = "Toyota";
$myCar->color = "Red";
echo $myCar->brand; // Output: Toyota
$myCar->start(); // Output: The car has started 🚗
?>
A constructor is a special method that runs automatically when you create an object.
<?php
class Car {
public $brand;
public $color;
public function __construct($brand, $color) {
$this->brand = $brand;
$this->color = $color;
}
}
$car1 = new Car("Honda", "Blue");
echo $car1->brand; // Honda
?>
Inheritance lets one class borrow features of another.
<?php
class Vehicle {
public function move() {
echo "This vehicle moves 🚙";
}
}
class Bike extends Vehicle {
public function ringBell() {
echo "Ring Ring! 🔔";
}
}
$bike = new Bike();
$bike->move(); // Inherited method
$bike->ringBell(); // Own method
?>
Encapsulation means protecting data by controlling access.
public
→ Accessible everywhereprotected
→ Accessible inside the class & subclassesprivate
→ Accessible only inside the class<?php
class BankAccount {
private $balance = 0;
public function deposit($amount) {
$this->balance += $amount;
}
public function getBalance() {
return $this->balance;
}
}
$account = new BankAccount();
$account->deposit(500);
echo $account->getBalance(); // 500
?>
Polymorphism allows methods to behave differently depending on the object.
<?php
class Animal {
public function makeSound() {
echo "Some sound";
}
}
class Dog extends Animal {
public function makeSound() {
echo "Woof 🐶";
}
}
class Cat extends Animal {
public function makeSound() {
echo "Meow 🐱";
}
}
$dog = new Dog();
$cat = new Cat();
$dog->makeSound(); // Woof
$cat->makeSound(); // Meow
?>
Abstraction hides unnecessary details and shows only what’s important.
<?php
abstract class Shape {
abstract public function area();
}
class Square extends Shape {
private $side;
public function __construct($side) {
$this->side = $side;
}
public function area() {
return $this->side * $this->side;
}
}
$square = new Square(4);
echo $square->area(); // 16
?>
Q1: Is OOP mandatory in PHP?
👉 No, but it’s highly recommended for bigger projects.
Q2: Can I mix OOP and procedural code?
👉 Yes, but for clean code, stick with one style in a project.
Q3: What’s the difference between a class and an object?
👉 A class is a blueprint, while an object is a real-world instance of that class.
OOP in PHP may sound overwhelming at first, but once you understand classes, objects, inheritance, and encapsulation, everything starts making sense. It’s like moving from messy notes to a well-organized notebook—you’ll thank yourself later!
👉 Now it’s your turn:
Try creating a Student Management System with classes like Student
, Course
, and Teacher
. Play around, experiment, and see how powerful OOP can be.
No comments yet. Be the first to comment!
Please log in to post a comment:
Sign in with Google