CACS254 Scripting Language

Scripting LanguageUnit 710 min read

OOP in PHP: Classes, Inheritance, Polymorphism, Interfaces & Abstract Classes

Unit 7 of Scripting Language covers PHP’s object-oriented programming (OOP) features—classes, inheritance, polymorphism, interfaces, and abstract classes—with real-world applications, code examples, and exam-focused traces of how these concepts work under the hood.

TAKEAWAYS:

  • PHP OOP uses classes (blueprints) and objects (instances) to organize code into reusable, modular components, mimicking real-world relationships (e.g., a BankAccount class with deposit() and withdraw() methods).
  • Inheritance lets child classes reuse parent class properties/methods (e.g., SavingsAccount inheriting from BankAccount), reducing redundancy and enforcing a hierarchy like Animal → Dog → Labrador.
  • Polymorphism enables methods to behave differently based on the object (e.g., calculateArea() for Circle vs. Square), visualized via method overriding or interfaces.
  • Abstract classes and interfaces define contracts (e.g., Shape abstract class forcing calculateArea()), ensuring consistency across unrelated classes like Rectangle and Triangle.
  • Real-world PHP OOP appears in eSewa’s payment processing (inheritance for PaymentMethod → KhaltiPayment), Ncell’s billing system (polymorphism for Discount types), and Nepal Stock Exchange (NEPSE) data validation (interfaces for DataValidator).
  • Exam questions test code implementation (e.g., overriding, interfaces) and conceptual differences (e.g., abstract vs. interface), so trace variable states and method calls step-by-step.

Core Concepts of OOP in PHP

1. Classes and Objects: The Building Blocks

Definition: A class is a blueprint for objects, defining properties (attributes) and methods (functions). An object is an instance of a class. Example:

class BankAccount {
    public $accountNumber;
    public $balance;

    public function deposit($amount) {
        $this->balance += $amount;
    }

    public function withdraw($amount) {
        if ($amount <= $this->balance) {
            $this->balance -= $amount;
        }
    }
}

$myAccount = new BankAccount(); // Object creation
$myAccount->deposit(1000);

Visual Trace:

Step myAccount State Action
Initialization accountNumber: null, balance: 0 new BankAccount()
After deposit(1000) accountNumber: null, balance: 1000 $myAccount->deposit(1000)

Real-World Analogy:

  • eSewa’s User class has properties like userId, email, and methods like login() or updateProfile(). Each user (e.g., "user123") is an object of this class.

2. Inheritance: Code Reusability

Definition: Inheritance allows a child class to inherit properties/methods from a parent class, promoting DRY (Don’t Repeat Yourself) code. Example:

class Vehicle {
    public $brand;
    public function displayBrand() {
        echo $this->brand;
    }
}

class Car extends Vehicle {
    public $model;
}

$myCar = new Car();
$myCar->brand = "Toyota";
$myCar->model = "Corolla";
$myCar->displayBrand(); // Output: Toyota

Visual Hierarchy:

classDiagram
    class Vehicle {
        +brand: string
        +displayBrand()
    }
    class Car {
        +model: string
    }
    Vehicle <|-- Car : Inherits

Real-World Use:

  • Pathao’s Vehicle class (parent) has calculateFare(), inherited by Bike and Car child classes, each overriding calculateFare() based on distance and vehicle type.

3. Polymorphism: One Interface, Many Forms

Types:

  1. Method Overriding: Child class redefines a parent method.
  2. Method Overloading: PHP doesn’t support it natively (use variable arguments).
  3. Interfaces: Contracts for classes to implement methods.

Example (Overriding):

class Shape {
    public function calculateArea() {
        return 0;
    }
}

class Circle extends Shape {
    public $radius;
    public function calculateArea() {
        return 3.14 * $this->radius * $this->radius;
    }
}

$circle = new Circle();
$circle->radius = 5;
echo $circle->calculateArea(); // Output: 78.5

Visual State After Override:

Class calculateArea() Method Output for radius=5
Shape Returns 0 0
Circle Returns 3.14 * r² 78.5

Real-World Use:

  • NEPSE’s Stock class has calculateValue(), overridden by EquityStock (shares) and DebtStock (bonds) to compute different valuation formulas.

4. Abstract Classes vs. Interfaces

Comparison Table:

Feature Abstract Class Interface
Instantiation Cannot be instantiated (abstract) Cannot be instantiated
Properties Can have properties (with values) Cannot have properties
Methods Can have abstract and concrete methods Only abstract methods
Inheritance Single inheritance Multiple inheritance allowed
Use Case Shared code + partial implementation Contract for unrelated classes

Example (Abstract Class):

abstract class Animal {
    abstract public function makeSound();
    public function sleep() {
        echo "Zzz...";
    }
}

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

Example (Interface):

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

class FileLogger implements Logger {
    public function log($message) {
        file_put_contents("log.txt", $message);
    }
}

Visual Contract:

classDiagram
    class Logger {
        <<interface>>
        +log(message: string)
    }
    class FileLogger {
        +log(message: string)
    }
    Logger <|.. FileLogger : Implements

Real-World Use:

  • Khalti’s PaymentGateway interface defines processPayment(), implemented by KhaltiProcessor and EsewaProcessor classes.

5. Access Modifiers: Controlling Visibility

PHP uses public, protected, and private to restrict access:

  • Public: Accessible everywhere.
  • Protected: Accessible within class and child classes.
  • Private: Accessible only within the class.

Example:

class User {
    private $password;
    protected $email;
    public $name;

    public function setPassword($pass) {
        $this->password = $pass; // Private access
    }
}

Visual Access Rules:

mindmap
  root((Access Modifiers))
    public
    protected
    private

Worked Example: Bank Loan System

Scenario: Design a system where Loan is an abstract class with calculateInterest(), overridden by HomeLoan and CarLoan classes. Use inheritance and polymorphism.

Code:

abstract class Loan {
    protected $principal;
    abstract public function calculateInterest();
}

class HomeLoan extends Loan {
    public function calculateInterest() {
        return $this->principal * 0.08; // 8% interest
    }
}

class CarLoan extends Loan {
    public function calculateInterest() {
        return $this->principal * 0.12; // 12% interest
    }
}

// Usage
$homeLoan = new HomeLoan();
$homeLoan->principal = 500000;
echo $homeLoan->calculateInterest(); // Output: 40000

Trace Table:

Class principal calculateInterest() Output
Loan N/A Abstract N/A
HomeLoan 500,000 500000 * 0.08 40,000
CarLoan 300,000 300000 * 0.12 36,000

Real-World Tie:

  • Nepal Bank Limited’s loan system uses this structure: Loan (abstract) → HomeLoan/CarLoan (concrete), with interest rates dynamically calculated based on loan type.

Common Pitfalls and Best Practices

  1. Avoid Deep Inheritance Hierarchies: Prefer composition over deep inheritance (e.g., use has-a instead of is-a where possible).
  2. Use Interfaces for Unrelated Classes: Example: Serializable interface for User and Product classes.
  3. Document Abstract Methods: Clearly state what child classes must implement (e.g., @method calculateArea() in Shape).
  4. Leverage Type Hinting: Ensure methods accept correct types (e.g., public function setAge(int $age)).

In the Real World

  1. eSewa’s Payment Processing:

    • Concept: Inheritance and Polymorphism.
    • How: The PaymentMethod abstract class defines processPayment(), implemented by child classes like KhaltiPayment (overrides with Khalti API calls) and CreditCardPayment (overrides with card validation). Polymorphism allows eSewa to call processPayment() generically, handling any payment type.
  2. Pathao’s Ride Fare Calculation:

    • Concept: Method Overriding.
    • How: The Vehicle class has calculateFare(), overridden by Bike (flat rate + distance) and Car (distance × base rate). When a user books a ride, Pathao’s system calls calculateFare() on the vehicle object, dynamically computing the correct fare.
  3. NEPSE’s Stock Data Validation:

    • Concept: Interfaces.
    • How: The DataValidator interface enforces validate() and format() methods. EquityValidator and DebtValidator implement these, ensuring all stock data adheres to NEPSE’s rules before processing trades.

Exam Tip

  1. Code Implementation Questions:

    • Always trace variable states after each method call (e.g., show $object->property values before/after operations).
    • For inheritance, explicitly state parent-child relationships in comments (e.g., // Car extends Vehicle).
  2. Conceptual Differences:

    • Abstract vs. Interface: Abstract classes provide partial implementation; interfaces are pure contracts. Example:
      • Abstract: Animal with sleep() implemented, makeSound() abstract.
      • Interface: Logger with only log() declared.
    • Overriding vs. Overloading: PHP doesn’t support overloading natively. Use variable arguments (...$args) or default parameters instead.
  3. Real-World Scenarios:

    • Link answers to Nepali tech companies (e.g., "Like Pathao’s fare system, this uses polymorphism to handle different vehicle types").
    • For CRUD operations, show SQL queries alongside PHP code (e.g., INSERT INTO users VALUES (...)).
  4. Common Exam Patterns:

    • Design a form + validate + store: Use isset()/empty() for validation, PDO for database operations.
    • CRUD Operations: Implement create(), read(), update(), delete() methods in a class (e.g., UserManager).

Pro Tip: For questions like "Write a PHP program to handle method overriding", include:

  1. A parent class with a method.
  2. A child class overriding it.
  3. A trace table showing method behavior before/after override.
  4. A real-world analogy (e.g., "Like Ncell’s billing system overrides base rates for loyal customers").

Based on the TU BCA syllabus for Scripting Language (CACS254), unit 7.

Discussion

Loading…