DEV Community

Ghulam Mujtaba
Ghulam Mujtaba

Posted on

PHP OOP Concepts: Classes, Objects and Inheritance

Class in PHP

A class in PHP is a blueprint or a template that defines the properties and behaviors of an object. It's a way to encapsulate data and functions that operate on that data. A class defines the structure and behavior of an object, including its properties (data) and methods (functions).


<?php 

class Employee {
  public $name;
  public $salary;

  public function __construct($name, $salary) {
    $this->name = $name;
    $this->salary = $salary;
  }

  public function getDetails() {
    echo "Name: $this->name, Salary: $this->salary";
  }
}
Enter fullscreen mode Exit fullscreen mode

Object in PHP

An object in PHP is an instance of a class, which represents a real-world entity or concept. It has its own set of attributes (data) and methods (functions) that describe and define its behavior. Objects are created from classes and can be manipulated independently.

$manager = new Manager();
$developer = new Developer();
Enter fullscreen mode Exit fullscreen mode

Inheritance in PHP

Inheritance in PHP is a mechanism that allows one class to inherit the properties and behaviors of another class. The inheriting class (child or subclass) inherits all the properties and methods of the parent class and can also add new properties and methods or override the ones inherited from the parent class.


//Inheritance 

class Manager extends Employee {
  public $department;

  public function __construct($name, $salary, $department) {
    parent::__construct($name, $salary);
    $this->department = $department;
  }

  public function getDetails() {
    parent::getDetails();
    echo ", Department: $this->department";
  }
}

class Developer extends Employee {
  public $specialty;

  public function __construct($name, $salary, $specialty) {
    parent::__construct($name, $salary);
    $this->specialty = $specialty;
  }

  public function getDetails() {
    parent::getDetails();
    echo ", Specialty: $this->specialty";
  }
}

// Create objects
$manager = new Manager("John Doe", 80000, "Marketing");
$developer = new Developer("Jane Smith", 70000, "Front-end");

// Access properties and methods
echo "Manager Details: ";
$manager->getDetails();
echo "\n";
echo "Developer Details: ";
$developer->getDetails();
Enter fullscreen mode Exit fullscreen mode

Each class has properties like name and salary, and methods like getDetails. The code creates objects from these classes and uses their properties and methods. In this we can see how classes can inherit and add new features, and how objects can be used to store and display data. We can check this code's output by running the project in the current console, and the output will be:

Manager Details: Name: John Doe, Salary: 80000, Department: Marketing
Developer Details: Name: Jane Smith, Salary: 70000, Specialty: Front-end
Enter fullscreen mode Exit fullscreen mode

I hope that you have clearly understood it

Top comments (0)