- Published on
PHP Oops Concept
- Authors
- Name
- Ganesh Negi
Oops Concept

Introduction
In object-oriented programming (OOP), classes and objects are key building blocks that help you structure your code in a clean, modular, and reusable way.
To explain this simply, let’s look at these concepts using PHP and a real-life example.

A class is like a blueprint or a plan for creating objects.
It defines the properties (also called attributes) and methods (functions) that the object will have.
In PHP, you create a class using the class keyword. Think of it like designing a car model — the class describes what every car of that model should have, like color, engine type, and the ability to start or stop.
<?php
class Cat {
public $name; // properties
public $breed; // properties
public function meow() { // method
echo "Woof! Woof!";
}
public function fetch() { // method
echo "{$this->name} is fetching.";
}
}
?>
In this example, we have created a Cat class with properties breed and methods meow() and fetch()
An object is a specific instance of a class — it's like a real-life version of the blueprint.
While a class defines the structure, the object is the actual thing you can work with.
You can create many objects from the same class, and each one can have its own unique values for the properties defined in the class.
<?php
// Creating objects from the Cat class
$cat1 = new Cat();
$cat2 = new Cat();
// Setting property values
$cat1->name = "Katty";
$cat1->breed = "Abyssinian";
$cat2->name = "Latty";
$cat2->breed = "Asian";
// Calling methods
$cat1->meow(); // Outputs: Woof! Woof!
$cat2->fetch(); // Outputs: Latty is fetching.
?>
In this example, we’ve created two objects — cat2 — from the Cat class.
Each of these objects has its own unique property values, but both can use the methods defined in the class.
This shows how you can create multiple, independent instances from a single class blueprint.
Four pillars of Oops

The four main pillars of Object-Oriented Programming (OOP) are abstraction, encapsulation, inheritance, and polymorphism.
These principles help make code more organized, flexible, and easier to maintain.
Let’s explore each of these concepts in detail with real-life examples to understand how they work in practice.
Abstraction
Abstraction in object-oriented programming (OOP) means hiding the complex inner workings of an object and showing only the important parts that are relevant to the user.
It helps you focus on what an object does, not how it does it.
In PHP, you can achieve abstraction using abstract classes and interfaces, which define a set of methods that must be implemented by any class that uses them — without revealing the actual code behind those methods.
Imagine you're developing a system to manage various shapes such as circles, rectangles, and triangles.
While all these shapes have an area, the method used to calculate that area differs for each one.
To handle this efficiently, you can use abstraction in PHP to define a common structure, while allowing each shape to implement its own specific logic for calculating the area.
Here's how you can do that using abstract classes.
// Abstract class representing a shape
abstract class Shape {
// Abstract method for calculating the area
abstract public function calculateArea();
}
class Circle extends Shape {
private $radius;
public function __construct($radius) {
$this->radius = $radius;
}
public function calculateArea() {
return pi() * pow($this->radius, 2);
}
}
class Rectangle extends Shape {
private $length;
private $width;
public function __construct($length, $width) {
$this->length = $length;
$this->width = $width;
}
public function calculateArea() {
return $this->length * $this->width;
}
}
$circle = new Circle(5);
$rectangle = new Rectangle(4, 6);
echo 'Circle Area: ' . $circle->calculateArea() . PHP_EOL;
echo 'Rectangle Area: ' . $rectangle->calculateArea() . PHP_EOL;
In this example, the Shape class is an abstract class that defines a common structure for all shapes — specifically, the method to calculate the area.
The Circle and Rectangle classes are concrete classes that extend the Shape class and implement their own unique logic for calculating the area based on their shape type.
This approach highlights how abstraction allows you to focus on the shared behavior while leaving the specific details to each subclass.
Encapsulation
Encapsulation is one of the four fundamental principles, along with inheritance, polymorphism, and abstraction.
Encapsulation refers to the bundling of data and the methods that operate on that data into a single unit, often called a class.
The main idea is to hide the internal details of how an object works and expose only what is necessary for the outside world to interact with it.
class Car {
// Private properties (attributes)
private $model;
private $color;
private $fuelLevel;
public function __construct($model, $color) {
$this->model = $model;
$this->color = $color;
$this->fuelLevel = 100;
}
// Public method to get the model of the car
public function getModel() {
return $this->model;
}
// Public method to get the color of the car
public function getColor() {
return $this->color;
}
// Public method to get the fuel level of the car
public function getFuelLevel() {
return $this->fuelLevel;
}
// Public method to simulate driving, reducing fuel level
public function drive() {
// Simulate driving by reducing fuel level
$this->fuelLevel -= 10;
if ($this->fuelLevel < 0) {
$this->fuelLevel = 0; // Ensure fuel level doesn't go below 0
}
}
}
$myCar = new Car("Toyota", "Blue");
echo "Model: " . $myCar->getModel() . "<br>";
echo "Color: " . $myCar->getColor() . "<br>";
echo "Fuel Level: " . $myCar->getFuelLevel() . "%<br>";
$myCar->drive();
echo "Updated Fuel Level after driving: " . $myCar->getFuelLevel() . "%";
In this example:
The Car class encapsulates its internal details like model, color, and fuelLevel by declaring them as private properties.
These internal properties are hidden from outside code, and can only be accessed or modified through public methods such as getModel(), setColor(), or refuel().
This not only protects the data but also provides a level of abstraction, allowing users to interact with the object without needing to know how everything works internally.
Inheritance
Inheritance is a concept where a new class, called the child or subclass, can inherit properties and methods from an existing class, called the parent or superclass.
This allows the subclass to reuse the functionality of the parent class and extend or modify it if needed.
It creates a relationship between classes, where the subclass gains the characteristics of the parent class while also having the ability to introduce its own unique features.
// Parent class
class Animal {
public $name;
public function __construct($name) {
$this->name = $name;
}
public function eat() {
return $this->name . ' is eating.';
}
}
// Child class inheriting from Animal
class Dog extends Animal {
public function bark() {
return $this->name . ' is barking.';
}
}
// Child class inheriting from Animal
class Cat extends Animal {
public function meow() {
return $this->name . ' is meowing.';
}
}
// Create instances of the classes
$dog = new Dog('Buddy');
$cat = new Cat('Whiskers');
// Using inherited methods
echo $dog->eat(); // Output: Buddy is eating.
echo $dog->bark(); // Output: Buddy is barking.
echo $cat->eat(); // Output: Whiskers is eating.
echo $cat->meow(); // Output: Whiskers is meowing.
In this example, we have a parent class Animal with a property $name and a method eat().
The Dog and Cat classes are child classes that inherit from the Animal class.
These child classes inherit the $name property and the eat() method from Animal, but they can also define their own unique methods (like bark() for Dog and meow() for Cat).
By using inheritance, we avoid duplicating common code across multiple classes, making the code more organized, efficient, and reusable.
This allows you to extend or modify functionality as needed without rewriting shared behavior.