Home >Backend Development >PHP Tutorial >Usage and examples of public keyword in PHP

Usage and examples of public keyword in PHP

WBOY
WBOYOriginal
2023-06-28 20:38:151642browse

The public keyword in PHP is an access modifier in object-oriented programming. It is used to indicate that members (properties and methods) are public and can be accessed from anywhere in the class. This article will introduce the usage and examples of the public keyword in PHP.

  1. Public modification of attributes

In PHP, we can use the public keyword to modify the attributes of a class so that it can be accessed from anywhere in the class. For example, we define a Person class and use the public keyword to modify its attribute $name:

class Person {
    public $name;
    
    public function __construct($name) {
        $this->name = $name;
    }
    
    public function greet() {
        echo "Hello, my name is " . $this->name;
    }
}

$person = new Person("John");
$person->name = "Mike";  // 修改属性值
echo $person->name;  // 访问属性值

In the above example, we can directly access and modify the $name attribute of the $person object because it is public Keyword modification. By calling the $person->greet() method, we can output "Hello, my name is Mike".

  1. Public modification of methods

In addition to attributes, we can also use the public keyword to modify class methods. Methods modified by public can be directly called by external code of the class. For example, we continue to use the above Person class and add a public modified sayHello method:

class Person {
    public $name;
    
    // 构造方法
    
    // 属性$name
    
    public function greet() {
        echo "Hello, my name is " . $this->name;
    }
    
    public function sayHello($to) {
        echo "Hello, " . $to;
    }
}

$person = new Person("John");
$person->sayHello("Mike");  // 调用public方法

In the above example, we can call the Person class through $person->sayHello("Mike") The public modified sayHello method in the method outputs "Hello, Mike".

  1. Inheritance of public modifiers

When a class inherits another class, the access modifiers of the inherited properties and methods follow the inheritance. If inherited properties or methods are public, they will also be public in subclasses. For example, we define a Student class that inherits from the Person class:

class Student extends Person {
    public $grade;
    
    public function study() {
        echo "I am studying in grade " . $this->grade;
    }
}

$student = new Student("Alice");
$student->name = "Bob";  // 从父类继承的public属性
echo $student->name;  // 从父类继承的public属性
$student->study();  // 调用从父类继承的public方法

In the above example, the Student class inherits the Person class, and can directly access and modify the public attribute $name inherited from the parent class, and call it from The public method greet inherited from the parent class.

Summary:

In PHP, the public keyword is used to define the access permissions of the properties and methods of a class as public and can be accessed from anywhere in the class. Properties and methods modified by public can be directly called and modified in the internal and external code of the class. In addition, the inheritance mechanism of the public modifier allows subclasses to inherit and use the public properties and methods in the parent class. By rationally using the public keyword, we can better encapsulate the code and improve the maintainability and reusability of the code.

The above is the usage and examples of the public keyword in PHP. I hope it will be helpful to readers in understanding and applying the public keyword.

The above is the detailed content of Usage and examples of public keyword in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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