Home > Backend Development > PHP Tutorial > Introduction to PHP object-oriented programming: how to define and use classes and their member functions

Introduction to PHP object-oriented programming: how to define and use classes and their member functions

WBOY
Release: 2023-07-29 15:40:02
Original
1498 people have browsed it

PHP Introduction to Object-Oriented Programming: How to define and use classes and their member functions

Introduction: Object-Oriented Programming (OOP for short) is a programming idea that divides problems into Multiple objects, and define the relationships and behaviors between objects to achieve modularization and reuse of code. As a popular web development language, PHP also supports object-oriented programming. This article will introduce how to define and use classes and their member functions in PHP.

1. Define the class and its member functions

In PHP, a class is defined by the keyword class. A class is an abstraction of objects, used to describe a class of objects with the same properties and behavior. The following is a simple class definition example:

class Person {
    // 成员变量
    public $name;
    private $age;

    // 构造函数
    public function __construct($name, $age) {
        $this->name = $name;
        $this->age = $age;
    }

    // 成员函数
    public function displayInfo() {
        echo "姓名:" . $this->name . "<br/>";
        echo "年龄:" . $this->age . "<br/>";
    }
}
Copy after login

In the above example, we define a class named Person. It contains two member variables $name and $age, as well as a constructor function __construct() and a member function displayInfo().

2. Instantiate objects

In PHP, if we want to use a class, we need to first create an instance (object) of the class. By using the new keyword and the class name, you can instantiate an object. The following is an example of instantiating the Person class:

$person = new Person("小明", 18);
Copy after login

In the above example, we create a Person object named $person and pass in the name and age as parameters of the constructor.

3. Call member functions

In PHP, by using the member operator -> of the object, we can access and call the member variables and member functions of the object. The following is an example of calling the member function of the Person object:

$person->displayInfo();
Copy after login

In the above example, we call the displayInfo() function of the Person object $person to display the name and age.

4. Access member variables

In PHP, through the member operator ->, we can access the public member variables of the object. However, if a member variable is declared private, it cannot be accessed directly. To access private member variables indirectly, we can use accessor (getter) and setter (setter) methods. The following is an example of using accessors and setters to access private member variables:

class Person {
    // ...

    // 获取姓名
    public function getName() {
        return $this->name;
    }

    // 设置年龄
    public function setAge($age) {
        if ($age >= 0) {
            $this->age = $age;
        }
    }

    // ...
}

$person = new Person("小明", 18);
echo "姓名:" . $person->getName() . "
"; $person->setAge(20); echo "年龄:" . $person->getAge() . "
";
Copy after login

In the above example, we obtain the value of the private member variable $name through the getName() function, and obtain the value of the private member variable $name through setAge() The function sets the value of the private member variable $age.

5. Summary

This article introduces the basic knowledge of defining and using classes and their member functions in PHP. By defining classes, instantiating objects, calling member functions and accessing member variables, we can flexibly use object-oriented programming. I hope this article can be helpful to readers who are new to object-oriented programming in PHP.

The above is the detailed content of Introduction to PHP object-oriented programming: how to define and use classes and their member functions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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