Lesson 11: Advanced OOP Concepts

In this lesson, you will learn advanced Object-Oriented Programming (OOP) concepts in PHP, including inheritance, polymorphism, abstract classes, and interfaces. These concepts are critical for building reusable, scalable, and modular PHP applications.


11.1 Inheritance

What is Inheritance?

  • Inheritance allows a class (child class) to inherit properties and methods from another class (parent class).
  • Promotes code reuse and helps in extending the functionality of existing classes.

Syntax

php
class ParentClass {
// Properties and methods
}
class ChildClass extends ParentClass {
// Additional properties and methods
}

Example: Inheritance

php
<?php
// Parent class
class Animal {
public $name;
public function eat() {
echo $this->name is eating.<br>”;
}
}

// Child class
class Dog extends Animal {
public function bark() {
echo $this->name is barking.<br>”;
}
}

// Creating an object of the child class
$dog = new Dog();
$dog->name = “Buddy”;
$dog->eat(); // Outputs: Buddy is eating.
$dog->bark(); // Outputs: Buddy is barking.
?>


Overriding Methods

  • A child class can override a method from the parent class to provide a new implementation.
php
<?php
class Animal {
public function makeSound() {
echo "Animal makes a sound.<br>";
}
}
class Cat extends Animal {
public function makeSound() {
echo “Cat meows.<br>”;
}
}

$cat = new Cat();
$cat->makeSound(); // Outputs: Cat meows.
?>


Using parent::

  • Use parent:: to call the parent class’s method inside the child class.
php
<?php
class Animal {
public function makeSound() {
echo "Animal makes a sound.<br>";
}
}
class Dog extends Animal {
public function makeSound() {
parent::makeSound(); // Call parent class method
echo “Dog barks.<br>”;
}
}

$dog = new Dog();
$dog->makeSound();
// Outputs:
// Animal makes a sound.
// Dog barks.
?>


11.2 Polymorphism

What is Polymorphism?

  • Polymorphism means “many forms.” It allows objects of different classes to be treated as objects of a common parent class.
  • Achieved through method overriding and interfaces.

Example: Polymorphism

php
<?php
class Animal {
public function makeSound() {
echo "Animal makes a sound.<br>";
}
}
class Dog extends Animal {
public function makeSound() {
echo “Dog barks.<br>”;
}
}

class Cat extends Animal {
public function makeSound() {
echo “Cat meows.<br>”;
}
}

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

$dog = new Dog();
$cat = new Cat();

makeAnimalSound($dog); // Outputs: Dog barks.
makeAnimalSound($cat); // Outputs: Cat meows.
?>


11.3 Abstract Classes

What is an Abstract Class?

  • An abstract class serves as a blueprint for other classes.
  • It cannot be instantiated directly.
  • It can have both abstract (unimplemented) and non-abstract (implemented) methods.

Syntax

php
abstract class AbstractClass {
abstract protected function abstractMethod();
public function concreteMethod() {
// Implemented method
}
}

Example: Abstract Class

php
<?php
abstract class Animal {
abstract public function makeSound(); // Abstract method
public function eat() {
echo “This animal is eating.<br>”; // Non-abstract method
}
}

class Dog extends Animal {
public function makeSound() {
echo “Dog barks.<br>”;
}
}

class Cat extends Animal {
public function makeSound() {
echo “Cat meows.<br>”;
}
}

$dog = new Dog();
$dog->makeSound(); // Outputs: Dog barks.
$dog->eat(); // Outputs: This animal is eating.

$cat = new Cat();
$cat->makeSound(); // Outputs: Cat meows.
?>


11.4 Interfaces

What is an Interface?

  • An interface defines a contract that implementing classes must follow.
  • All methods in an interface are abstract by default.
  • A class can implement multiple interfaces, allowing for multiple inheritances.

Syntax

php
interface InterfaceName {
public function methodName();
}
class ClassName implements InterfaceName {
public function methodName() {
// Implementation
}
}


Example: Interface

php
<?php
interface Animal {
public function makeSound();
}
class Dog implements Animal {
public function makeSound() {
echo “Dog barks.<br>”;
}
}

class Cat implements Animal {
public function makeSound() {
echo “Cat meows.<br>”;
}
}

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

$cat = new Cat();
$cat->makeSound(); // Outputs: Cat meows.
?>


Extending Multiple Interfaces

php
<?php
interface Animal {
public function eat();
}
interface Pet {
public function play();
}

class Dog implements Animal, Pet {
public function eat() {
echo “Dog is eating.<br>”;
}

public function play() {
echo “Dog is playing.<br>”;
}
}

$dog = new Dog();
$dog->eat(); // Outputs: Dog is eating.
$dog->play(); // Outputs: Dog is playing.
?>


Practical Example: Combining Concepts

Example: Shape Class

php
<?php
abstract class Shape {
abstract public function calculateArea();
}
class Rectangle extends Shape {
private $width;
private $height;

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

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

class Circle extends Shape {
private $radius;

public function __construct($radius) {
$this->radius = $radius;
}

public function calculateArea() {
return pi() * $this->radius * $this->radius;
}
}

$shapes = [
new Rectangle(5, 10),
new Circle(7)
];

foreach ($shapes as $shape) {
echo “Area: “ . $shape->calculateArea() . “<br>”;
}
?>


Activities and Exercises

  1. Inheritance:
    • Create a Vehicle class and a Car class that extends it. Add methods for starting and stopping the vehicle.
  2. Polymorphism:
    • Implement a function that takes a Shape object (Rectangle, Circle, etc.) and prints its area.
  3. Abstract Classes:
    • Create an abstract class Payment with an abstract method processPayment(). Implement it in classes for CreditCardPayment and PaypalPayment.
  4. Interfaces:
    • Create an interface Logger with a method logMessage(). Implement the interface in classes for FileLogger and DatabaseLogger.

Assignment

  1. Create an abstract class Employee with:
    • Properties: name, salary.
    • Abstract method calculateBonus().
  2. Implement two child classes:
    • Manager with a bonus of 20% of the salary.
    • Developer with a bonus of 10% of the salary.
  3. Create a script to calculate and display the bonus for both managers and developers.

Summary

In this lesson, you learned:

  1. How to use inheritance to extend functionality.
  2. How polymorphism allows different objects to be treated uniformly.
  3. How abstract classes and interfaces define reusable blueprints for classes.

These advanced concepts form the backbone of scalable and maintainable PHP applications. Let me know if you need more examples or exercises!


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *