PHP OOP Concepts: Classes, Objects and Inheritance

WBOY
Release: 2024-07-24 09:08:14
Original
627 people have browsed it

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";
  }
}
Copy after login

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();
Copy after login

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();
Copy after login

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
Copy after login

I hope that you have clearly understood it

The above is the detailed content of PHP OOP Concepts: Classes, Objects and Inheritance. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!