How to use class in php

小老鼠
Release: 2023-04-21 13:22:26
Original
2088 people have browsed it

Class in php is a mechanism for encapsulating data and behavior. How to use it: 1. Create a php sample file; 2. Use the "class" keyword to define a "Person" class, the syntax is "" class Person{}"; 3. Set two attributes for the "Person" class, namely "$name" and "$age"; 4. Define the "sayHello()" method through the public modifier; 5. Inside the Class or Just access the set properties externally.

How to use class in php

Operating system for this tutorial: Windows 10 system, PHP version 8.1.3, Dell G3 computer.

PHP Class is the core concept in OOP, which provides a mechanism to encapsulate data and behavior. These Classes provide us with reusable code, reduce code redundancy and improve code maintainability. This article will introduce PHP Class The basic usage and importance of .

1. The concept and definition of PHP Class

PHP Class is a mechanism that encapsulates data and behavior. It defines the data and a collection of methods. Class definitions can include variable and function definitions, and we can think of them as class attributes and class methods. In PHP, we use the keyword "class" to define a class.

For example, The following is a sample code that defines a Class:

class Person {
   // 定义变量
   public $name;
   public $age;
  
   // 定义方法
   public function sayHello() {
      echo "Hello, my name is " . $this->name . " and I am " . $this->age . " years old.";
   }
}
Copy after login

In the above code, we define a Class named "Person". This Class has two attributes: $name and $age, and There is a method: sayHello(). Both properties are public access control modifiers. This means that these properties can be accessed inside or outside the Class. $this is referenced in the sayHello() method, which is a self-reference and represents the current instance .

2. Creation and use of PHP Class

Creating a PHP Class object can be achieved through the "new" keyword. After creating the object, we Its methods and properties can be used. Below is an example of instantiating a Person Class.

// 实例化一个Person对象
$person1 = new Person();
  
// 设置对象的属性
$person1->name = "John";
$person1->age = 20;
  
// 调用对象的方法
$person1->sayHello();
Copy after login

In the above code, we instantiate a $person1 object and then set the $name and $age properties . Finally, we called the sayHello() method, which outputs the values ​​of the attributes $name and $age.

You can also use "new" before the definition of Class to create an object.

$person = new Person;
Copy after login

3. Inheritance of PHP Class

PHP Class can share properties and methods with other Classes through inheritance (Inheritance). Subclasses (or derived classes) can use the properties and methods of the parent class, or they can define their own properties and methods.

// 定义Employee类,继承Person类
class Employee extends Person {
   public $position;
  
   public function jobPosition() {
      echo "I am a/an " . $this->position;
   }
}
Copy after login

In the above code, we define a Class named "Employee", which extends the "Person" Class. The Employee class has a new property $position and a new method jobPosition(). In the jobPosition() method, $this->position refers to the property $position of the subclass.

The above is the detailed content of How to use class in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
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!