Home / PHP

Classes, Objects, and Inheritance in PHP

PHP is a versatile programming language that allows developers to create complex and scalable applications. One of the main reasons for PHP's popularity is its powerful object-oriented programming (OOP) capabilities, which enable the use of classes, objects, and inheritance.

Classes and Objects

In PHP, a class is a blueprint for creating objects. It defines the properties and methods that an object of the class will have. To create a class, you can use the class keyword followed by the name of the class. Let's consider an example of a Car class:

class Car {
    // Properties
    public $brand;
    public $model;
    public $color;

    // Methods
    public function startEngine() {
        echo "Engine started!";
    }

    public function accelerate() {
        echo "Accelerating...";
    }

    public function brake() {
        echo "Braking...";
    }
}

To create an object from a class, you can use the new keyword followed by the name of the class and parentheses. For example:

$myCar = new Car();

Now, $myCar is an object of the Car class. You can access its properties and call its methods using the arrow operator (->). For instance:

$myCar->brand = "Toyota";
$myCar->model = "Camry";
$myCar->color = "Blue";

$myCar->startEngine();  // Output: Engine started!
$myCar->accelerate();   // Output: Accelerating...
$myCar->brake();        // Output: Braking...

Inheritance

Inheritance is a fundamental concept in OOP that allows a class (called the child or derived class) to inherit properties and methods from another class (called the parent or base class). PHP supports single inheritance, meaning a child class can only inherit from a single parent class.

To demonstrate inheritance, let's consider an example where we have a Sedan class that extends the Car class:

class Sedan extends Car {
    // Additional properties specific to Sedan class
    public $trunkCapacity;

    // Additional methods specific to Sedan class
    public function openTrunk() {
        echo "Trunk opened!";
    }
}

Now, the Sedan class inherits all the properties and methods of the Car class. Additionally, it can have its own specific properties and methods. You can create objects from the Sedan class and access both inherited and specific properties/methods. For example:

$sedan = new Sedan();

// Inherited properties from Car class
$sedan->brand = "Honda";
$sedan->model = "Civic";
$sedan->color = "Red";

// Specific property of Sedan class
$sedan->trunkCapacity = 500;

// Accessing inherited methods from Car class
$sedan->startEngine();   // Output: Engine started!
$sedan->accelerate();    // Output: Accelerating...
$sedan->brake();         // Output: Braking...

// Accessing specific method of Sedan class
$sedan->openTrunk();     // Output: Trunk opened!

In this example, the Sedan class inherits properties like $brand, $model, and $color from the Car class. It also has its own specific property $trunkCapacity and a specific method openTrunk().

Conclusion

Classes, objects, and inheritance are essential concepts in PHP's object-oriented programming paradigm. By utilizing classes and objects, you can create reusable and modular code. With inheritance, you can build hierarchies of related classes and promote code reusability, maintainability, and extensibility.

Understanding classes, objects, and inheritance in PHP opens up a world of possibilities for building robust and efficient applications. So, dive in, explore, and leverage the power of OOP in PHP to take your coding skills to the next level!


noob to master © copyleft