PHP Object Oriented Programming

📘 PHP 👁 35 views 📅 Dec 22, 2025
⏱ Estimated reading time: 2 min

What is OOP in PHP?

Object-Oriented Programming (OOP) is a programming paradigm based on the concept of objects, which contain properties (variables) and methods (functions). PHP supports full OOP features to build scalable, maintainable, and reusable applications.


Advantages of OOP in PHP

  • Better code organization

  • Reusability using classes

  • Easy maintenance

  • Improved security

  • Real-world modeling


Class and Object in PHP

Class Definition

class Car { public $brand; public function showBrand() { echo $this->brand; } }

Creating an Object

$car = new Car(); $car->brand = "Toyota"; $car->showBrand();

Properties and Methods

  • Properties → Variables inside a class

  • Methods → Functions inside a class

class User { public $name; public function greet() { echo "Hello " . $this->name; } }

Constructor and Destructor

Constructor

Automatically called when an object is created.

class Person { public $name; function __construct($name) { $this->name = $name; } }

Destructor

Called when the object is destroyed.

function __destruct() { echo "Object destroyed"; }

Access Modifiers

PHP provides three access modifiers:

ModifierAccess Level
publicAccessible everywhere
protectedWithin class & child class
privateWithin same class only

Inheritance

Inheritance allows a child class to acquire properties and methods of a parent class.

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

Method Overriding

Child class can override parent class methods.

$dog = new Dog(); $dog->sound();

Encapsulation

Encapsulation hides data using access modifiers and getter/setter methods.

class Account { private $balance = 0; public function deposit($amount) { $this->balance += $amount; } public function getBalance() { return $this->balance; } }

Abstraction

Abstract classes cannot be instantiated.

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

Interfaces

Interfaces define method signatures.

interface Payment { public function pay(); } class CreditCard implements Payment { public function pay() { echo "Payment done"; } }

Polymorphism

Same method behaves differently based on object type.

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

Static Properties and Methods

class Math { public static function add($a, $b) { return $a + $b; } } echo Math::add(5, 3);

Namespaces

Used to avoid class name conflicts.

namespace App\Models; class User {}

Magic Methods

Some commonly used magic methods:

  • __construct()

  • __destruct()

  • __get()

  • __set()

  • __toString()


Best Practices for PHP OOP

  • Follow single responsibility principle

  • Use namespaces

  • Prefer composition over inheritance

  • Keep classes small and focused

  • Use type declarations


Conclusion

PHP OOP enables developers to write clean, modular, and maintainable code. Understanding OOP concepts is essential for working with modern PHP frameworks like Laravel and CodeIgniter.


🔒 Some advanced sections are available for Registered Members
Register Now

Share this Post


← Back to Tutorials

Popular Competitive Exam Quizzes