Object-Oriented Programming (OOP) in PHP allows for organizing code into reusable structures called classes and objects. OOP principles help create more modular and maintainable code.

Classes and Objects

Classes are blueprints for creating objects, which are instances of classes.

Class file Car.php and Index.php must be at the same folder

  • Defining a Class in a file:

Car.php

  <?php
class Car {
    // Properties
    public $color;
    public $model;

    // Method
    public function start() {
        return "The car is starting.";
    }
}
?>
  
  • Creating an Object:

Index.php

  <?php
include 'Car.php';
$myCar = new Car();
$myCar->color = "red";
$myCar->model = "Toyota";
echo $myCar->start(); // Outputs: The car is starting.
?>
  
  • Defining many Classes in one file:

Index.php

  class Car
{
    // Method
    public function start() {
        return "The car is starting.";
    }
}

class Bike
{
    public function ride() {
        return "Ride this bick";
    }
}

$myCar = new Car();
echo $myCar->start();

$myBike = new Bike();
echo $myBike->ride();
  

Properties and Methods

Properties represent the data of a class, while methods define the behavior.

  • Defining Properties:
  Copy code
<?php
class Car {
    public $color;
    private $model; // Private property
}
?>
  
  • Defining Methods:
  <?php
class Car {
    public function start() {
        return "The car is starting.";
    }

    private function stop() {
        return "The car is stopping.";
    }
}
?>
  

Inheritance

Inheritance allows a class to use methods and properties of another class. Class file Vehicle.php, Car.php and Index.php must be at the same folder.

  • Defining a Parent Class:

Vehicle.php

  <?php
class Vehicle {
    public function start() {
        return "Vehicle is starting.";
    }
}
?>
  
  • Defining a Child Class:

Car.php

  <?php
Include 'Vehicle.php';
class Car extends Vehicle {
    public function honk() {
        return "Car is honking.";
    }
}

?>
  
  • Index.php;

Index.php

  Include 'Car.php';

$myCar = new Car();
echo $myCar->start(); // Inherited method
echo $myCar->honk(); // Child class method
  

Polymorphism

Polymorphism allows methods to perform differently based on the object calling them.

  • Overriding Methods:
  <?php
class Vehicle {
    public function start() {
        return "Vehicle is starting.";
    }
}

class Car extends Vehicle {
    public function start() {
        return "Car is starting.";
    }
}

$myCar = new Car();
echo $myCar->start(); // Outputs: Car is starting.
?>
  
  • Method Overloading (Not directly supported in PHP): PHP does not support method overloading in the same way as other languages, but you can achieve similar functionality using default arguments or variable arguments.

Encapsulation

Encapsulation is the practice of hiding the internal state of an object and requiring all interaction to be performed through an object’s methods.

  • Using Access Modifiers:

    • Public: Accessible from everywhere
    • Protected: Accessible within the class and subclasses
    • Private: Accessible only within the class
      <?php
    class Car {
        private $fuel;
    
        public function __construct($fuel) {
            $this->fuel = $fuel;
        }
    
        public function getFuel() {
            return $this->fuel;
        }
    
        public function setFuel($fuel) {
            $this->fuel = $fuel;
        }
    }
    
    $myCar = new Car(100);
    echo $myCar->getFuel(); // Outputs: 100
    ?>
      

Object-Oriented Programming in PHP promotes code reusability and organization through classes, inheritance, polymorphism, and encapsulation. Mastering these OOP principles can greatly enhance the structure and maintainability of your code.