Current Location: Home> Latest Articles> Detailed Explanation of PHP Access Modifiers: Understanding public, protected, and private

Detailed Explanation of PHP Access Modifiers: Understanding public, protected, and private

M66 2025-07-26

Introduction to PHP Access Modifiers

In PHP development, access control is an essential means to ensure code security and maintain a clear structure. PHP defines access permissions for class properties and methods through modifiers: public, protected, and private. Understanding and properly using these modifiers is crucial for writing high-quality object-oriented code. This article explains these three access modifiers in detail, accompanied by code examples to help you master them comprehensively.

Using the public Modifier

The public modifier indicates public access permission, meaning the properties or methods can be accessed both inside and outside the class. It is typically used for functionality intended to be exposed externally. Here is an example:

class Person {
  public $name;

  public function sayHello() {
    echo 'Hello! My name is ' . $this->name;
  }
}

$person = new Person();
$person->name = 'Tom';
$person->sayHello();

In the example above, both the $name property and sayHello() method are declared public, so they can be accessed and called directly from outside the class instance.

Using the protected Modifier

The protected modifier indicates protected access permission, allowing access only within the class itself and its subclasses, preventing external direct access and modification. It is suitable for internal logic that requires inheritance but should not be freely called externally. Here is an example:

class Person {
  protected $name;

  protected function sayHello() {
    echo 'Hello! My name is ' . $this->name;
  }
}

class Student extends Person {
  public function introduce() {
    echo 'I am ' . $this->name . ', from the planet Avatar';
  }
}

$student = new Student();
$student->name = 'Lucy';  // Will cause an error, cannot access protected property directly
$student->introduce();

In this example, the $name property and sayHello() method are protected. The Student class can access them, but external code cannot access $student->name directly, ensuring data encapsulation.

Using the private Modifier

The private modifier indicates private access permission, meaning the properties and methods are accessible only within the class itself. Neither subclasses nor external code can access them. It is used to achieve strict encapsulation and hide internal details. Here is an example:

class Person {
  private $name;

  private function sayHello() {
    echo 'Hello! My name is ' . $this->name;
  }
}

$person = new Person();
$person->name = 'Lily';  // Will cause an error, cannot access private property
$person->sayHello();     // Will cause an error, cannot access private method

In this example, both $name and sayHello() are private, so they cannot be accessed by external code or subclasses, ensuring complete encapsulation of data and methods.

Summary

PHP access modifiers correspond to different access levels: public (open), protected (restricted), and private (strictly private). Proper use of these modifiers enhances code security and maintainability by preventing unnecessary access and erroneous modifications, making the program more robust. Hopefully, this article’s explanations and examples will help you better understand and use PHP access modifiers.