PHP OOP 개념: 클래스, 객체 및 상속

WBOY
풀어 주다: 2024-07-24 09:08:14
원래의
522명이 탐색했습니다.

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

위 내용은 PHP OOP 개념: 클래스, 객체 및 상속의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

원천:dev.to
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!