"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > PHP OOP Concepts: Classes, Objects and Inheritance

PHP OOP Concepts: Classes, Objects and Inheritance

Published on 2024-07-31
Browse:389

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).

name = $name;
    $this->salary = $salary;
  }

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

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();

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();

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

I hope that you have clearly understood it

Release Statement This article is reproduced at: https://dev.to/ghulam_mujtaba_247/php-oop-concepts-classes-objects-and-inheritance-44j9?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3