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 Concept | Description | Keyword Used |
Class & Object | Blueprint for creating objects | class, new |
Encapsulation | Hiding data using access modifiers | private, protected, public |
Inheritance | Reuse methods from another class | extends |
Polymorphism | Methods behave differently in different classes | override, interface |
Abstraction | Hiding implementation details | abstract, interface |
Static Methods | Belong to class, not object | static |
Conclusion
OOP in PHP provides a powerful way to organize and structure code using classes and objects. It promotes modular, reusable, and maintainable code.