Skip to content

Introduction to PHP OOPs concepts

Object-Oriented Programming (OOP) is a programming paradigm that organizes code into objects, making it modular, reusable, and scalable. PHP supports OOP features like classes, objects, inheritance, polymorphism, encapsulation, and abstraction.


1. Key OOP Concepts in PHP

βœ… 1.1 Class & Object

  • Class: A blueprint/template for creating objects.
  • Object: An instance of a class with its own properties and methods.

Example: Creating a Class & Object

<?php

// Define a class

class Car {

    public $brand; // Property

    // Constructor to initialize properties

    public function __construct($brand) {

        $this->brand = $brand;

    }

    // Method (Function)

    public function showBrand() {

        return “Car brand is ” . $this->brand;

    }

}

// Create an object

$myCar = new Car(“Toyota”);

// Access methods

echo $myCar->showBrand();

?>

Output:
πŸš— Car brand is Toyota


βœ… 1.2 Properties & Methods

  • Properties: Variables inside a class (public $name;).
  • Methods: Functions inside a class (function getName() {}).

Example

<?php

class Person {

    public $name;

    public function setName($newName) {

        $this->name = $newName;

    }

    public function getName() {

        return $this->name;

    }

}

$person1 = new Person();

$person1->setName(“Alice”);

echo $person1->getName();

?>

Output:
πŸ‘© Alice


βœ… 1.3 Encapsulation (Access Modifiers)

Encapsulation restricts direct access to certain class properties and methods. PHP provides three access modifiers:

  • public β†’ Accessible from anywhere.
  • protected β†’ Accessible within the class & child classes.
  • private β†’ Accessible only within the class.

Example: Encapsulation in PHP

<?php

class BankAccount {

    private $balance = 0; // Private property

    public function deposit($amount) {

        $this->balance += $amount;

    }

    public function getBalance() {

        return $this->balance;

    }

}

$account = new BankAccount();

$account->deposit(1000);

echo “Balance: $” . $account->getBalance();

?>

Output:
πŸ’° Balance: $1000
πŸ”’ Direct access ($account->balance = 500;) will cause an error!


βœ… 1.4 Inheritance

Inheritance allows a class to reuse properties and methods from another class (extends keyword).

Example: Parent & Child Class

<?php

class Animal {

    public function makeSound() {

        return “Some sound”;

    }

}

// Dog class inherits from Animal

class Dog extends Animal {

    public function makeSound() {

        return “Bark”;

    }

}

$dog = new Dog();

echo $dog->makeSound();

?>

Output:
🐢 Bark
πŸ”„ The Dog class overrides the makeSound() method.


βœ… 1.5 Polymorphism

Polymorphism allows a method to have different behaviors in different classes.

Example: Method Overriding

<?php

class Shape {

    public function area() {

        return “Calculating area…”;

    }

}

class Circle extends Shape {

    public function area() {

        return “Area = Ο€ Γ— rΒ²”;

    }

}

class Square extends Shape {

    public function area() {

        return “Area = side Γ— side”;

    }

}

$circle = new Circle();

$square = new Square();

echo $circle->area(); // Output: Area = Ο€ Γ— rΒ²

echo “<br>”;

echo $square->area(); // Output: Area = side Γ— side

?>


βœ… 1.6 Abstraction

Abstraction hides implementation details and only exposes functionality using abstract classes and interfaces.

Example: Abstract Class

<?php

abstract class Vehicle {

    abstract public function move(); // Abstract method

}

class Car extends Vehicle {

    public function move() {

        return “Car is moving”;

    }

}

$car = new Car();

echo $car->move();

?>

Output:
πŸš— Car is moving
πŸ”’ abstract classes cannot be instantiated directly.


βœ… 1.7 Interface

Interfaces define method signatures that implementing classes must define.

Example: Interface in PHP

<?php

interface Animal {

    public function sound(); // Method signature only

}

class Cat implements Animal {

    public function sound() {

        return “Meow”;

    }

}

$cat = new Cat();

echo $cat->sound();

?>

Output:
🐱 Meow
πŸ“Œ Interfaces do not contain method implementationsβ€”only definitions.


βœ… 1.8 Constructor & Destructor

  • Constructor (__construct) initializes object properties.
  • Destructor (__destruct) executes when the object is destroyed.

Example

<?php

class MyClass {

    public function __construct() {

        echo “Object created!<br>”;

    }

    public function __destruct() {

        echo “Object destroyed!”;

    }

}

$obj = new MyClass();

?>

Output:
πŸ›  Object created!
πŸ—‘ Object destroyed! (When script execution ends).


βœ… 1.9 Static Methods & Properties

  • Static properties/methods belong to a class, not an object.
  • Use self:: inside the class and ClassName:: outside.

Example: Static Property & Method

<?php

class Math {

    public static $pi = 3.14159;

    public static function square($num) {

        return $num * $num;

    }

}

// Access without creating an object

echo Math::$pi; // 3.14159

echo “<br>”;

echo Math::square(5); // 25

?>


βœ… 2. Advantages of OOP in PHP

βœ… Code Reusability – Inheritance allows sharing code across multiple classes.
βœ… Modularity – Objects and classes make code organized & structured.
βœ… Scalability – Easier to extend functionality without modifying existing code.
βœ… Security – Encapsulation hides sensitive data.


βœ… 3. Summary Table of OOP Concepts in PHP

OOP ConceptDescriptionKeyword Used
Class & ObjectBlueprint for creating objectsclass, new
EncapsulationHiding data using access modifiersprivate, protected, public
InheritanceReuse methods from another classextends
PolymorphismMethods behave differently in different classesoverride, interface
AbstractionHiding implementation detailsabstract, interface
Static MethodsBelong to class, not objectstatic

Conclusion

OOP in PHP provides a powerful way to organize and structure code using classes and objects. It promotes modular, reusable, and maintainable code.