object-oriented-programming

Understanding OOP in PHP

  • Profile picture of Mcs
  • by Mcs July 1, 2025

Introduction

PHP has long been associated with procedural programming, but modern PHP supports Object-Oriented Programming (OOP) — a powerful way to structure and organize your code. In this post, we’ll explore the basics of OOP in PHP with simple examples to help you get started.

What is Object-Oriented Programming?

Object-Oriented Programming (OOP) is a programming style that revolves around objects. An object is an instance of a class — a blueprint that defines properties and methods.

Think of a class as a "template", and an object as something created from that template.

1. Classes and Objects

A class defines the structure and behavior of something, and an object is an actual instance of that class.

class Car {
   public $color;

   public function drive() {
       echo "The car is driving.";
   }
}

$myCar = new Car();
$myCar->color = "Red";
$myCar->drive(); // Outputs: The car is driving.

2. Encapsulation

Encapsulation means hiding the internal details of an object and exposing only what’s necessary through methods.

class Person {
   private $name;

   public function setName($name) {
       $this->name = $name;
   }

   public function getName() {
       return $this->name;
   }
}

$p = new Person();
$p->setName("Alice");
echo $p->getName(); // Outputs: Alice

Here, the $name property is private, meaning it can't be accessed directly from outside the class.

3. Inheritance

Inheritance allows a class (child) to inherit properties and methods from another class (parent).

class Animal {
   public function makeSound() {
       echo "Some sound";
   }
}

class Dog extends Animal {
   public function makeSound() {
       echo "Bark";
   }
}

$dog = new Dog();
$dog->makeSound(); // Outputs: Bark

4. Polymorphism

Polymorphism lets you use the same method name in different classes with different behaviors.

class Cat {
   public function sound() {
       echo "Meow";
   }
}

class Bird {
   public function sound() {
       echo "Chirp";
   }
}

function makeSound($animal) {
   $animal->sound();
}

makeSound(new Cat());   // Meow
makeSound(new Bird());  // Chirp

5. Abstraction

Abstraction hides complex implementation details and only shows the essential features.

abstract class Shape {
   abstract public function area();
}

class Rectangle extends Shape {
   private $width, $height;

   public function __construct($w, $h) {
       $this->width = $w;
       $this->height = $h;
   }

   public function area() {
       return $this->width * $this->height;
   }
}

$rect = new Rectangle(4, 5);
echo $rect->area(); // Outputs: 20

6. Interfaces

Interfaces define a contract — any class that implements an interface must define its methods.

interface Logger {
   public function log($message);
}

class FileLogger implements Logger {
   public function log($message) {
       echo "Logging: $message";
   }
}

Benefits of OOP in PHP

  • Reusability: Code can be reused using inheritance.
  • Maintainability: Code is easier to update and debug.
  • Security: Encapsulation helps in securing data.
  • Extensibility: New features can be added easily using polymorphism and inheritance.

Conclusion

Learning OOP is a big step toward writing better and cleaner PHP code. Start small, build classes for common features, and slowly refactor your procedural code into OOP. Once you master these basics, frameworks like Laravel will become much easier to understand.

Comments

Add new comment

Restricted HTML

  • Allowed HTML tags: <br> <p> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id> <cite> <dl> <dt> <dd> <a hreflang href> <blockquote cite> <ul type> <ol type start> <strong> <em> <code> <li>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.