PHP is a versatile language that allows developers to work with both procedural and object oriented paradigms. As the complexity of applications grows using object-oriented programming becomes essential.

Creating Classes and Objects in PHP

Object Oriented Paradigm uses the concept of “objects”. Objects are the instances of classes which can be considered as blueprints or templates used to create objects.

Defining Class

class Car {
    // Properties
    public $make;
    public $model;
    public $year;

    // Constructor
    public function __construct($make, $model, $year) {
        $this->make = $make;
        $this->model = $model;
        $this->year = $year;
    }

    // Method
    public function displayInfo() {
        echo "This is a {$this->year} {$this->make} {$this->model}.";
    }
}

Creating an Object

$car = new Car("Maruti", "800", 2000);
$car->displayInfo();

Inheritance

Inheritance is one of the core features of OOP. It allows a class to inherit properties of a parent class. This helps promote code reusability and easier to maintain and extend the functionality.

// Parent class
class Vehicle {
    public $make;
    public $model;

    public function __construct($make, $model) {
        $this->make = $make;
        $this->model = $model;
    }

    public function startEngine() {
        echo "Engine started.";
    }
}

// Child class 
class Car2 extends Vehicle {
    public $year;

    public function __construct($make, $model, $year) {
        parent::__construct($make, $model); // parent constructor
        $this->year = $year;
    }

    // Method overriding
    public function startEngine() {
        echo "The car's engine started.\n";
    }

    public function displayInfo() {
        echo "This is a {$this->year} {$this->make} {$this->model}.\n";
    }
}

$car = new Car2("Toyota", "Corolla", 2018);
$car->startEngine();

Traits

PHP only allows single inheritance, so to using traits allows a method to use multiple methods from multiple traits which provides a method.

Traits are used to declare methods that can be used in multiple classes. Traits can have methods and abstract methods that can be used in multiple classes, and the methods can have any access modifier (public, private, or protected).

// Traits
trait Logger {
    public function log($message) {
        echo "Log: $message\n";
    }
}

class Car3 {
    use Logger; // Include the Logger trait

    public function drive() {
        $this->log("Car is driving.");
    }
}

class Bike {
    use Logger;

    public function ride() {
        $this->log("Bike is riding.");
    }
}

$car = new Car3();
$car->drive();

$bike = new Bike();
$bike->ride();

Abstract Classes

An abstract class is a class that cannot directly be instantiated. It is used to extend a class by providing methodsโ€”with method body like usual methods and with just the method definition.

abstract class Shape {
    protected $color;
    abstract public function area();

    public function setColor($color) {
        $this->color = $color;
    }
}

class Circle extends Shape {
    private $radius;

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

    // Implementing abstract method
    public function area() {
        return pi() * $this->radius * $this->radius;
    }
}

$circle = new Circle(5);
$circle->setColor("Red");
echo "Area: " . $circle->area();

Interfaces

Interfaces in PHP is like a contract that a class must follow, it specifies a method that classes must implement. A class can implement multiple interfaces. It can be used as a powerful tool to enforce consistent behaviour across the different classes.

interface Drawable {
    public function draw();
}

interface Movable {
    public function move();
}

class Car implements Drawable, Movable {
    public function draw() {
        echo "Drawing a car.";
    }

    public function move() {
        echo "Moving the car.";
    }
}

class Bike implements Drawable, Movable {
    public function draw() {
        echo "Drawing a bike.";
    }

    public function move() {
        echo "Moving the bike.";
    }
}

$car = new Car();
$car->draw();
$car->move();

$bike = new Bike();
$bike->draw();
$bike->move();

Conclusion

PHP’s Object Oriented Programming features like โ€” Inheritance, Traits, Abstract Classes, and Interfaces help developers write clean, maintainable, and reusable code.

  • Inheritance allows classes to share properties and methods.
  • Traits provide a way to reuse code across multiple classes without inheritance.
  • Abstract Classes define common behavior for child classes but cannot be instantiated on their own.
  • Interfaces ensure classes implement specific methods, promoting consistency across your codebase.

Leave a Reply

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