In PHP development, Object-Oriented Programming (OOP) is a widely adopted paradigm. By structuring code into classes and objects, OOP helps organize logic, enhance maintainability, and simplify scalability. To safeguard the internal state of a class and enforce encapsulation, PHP provides access modifiers.
In this article, we’ll explore the three main access modifiers in PHP—public, private, and protected—and demonstrate their usage with clear examples.
Access modifiers define the visibility and accessibility of class properties and methods. They are essential for encapsulating data and controlling how external code interacts with class internals. PHP supports three visibility levels:
public: Accessible from anywhere.
private: Accessible only within the class itself.
protected: Accessible within the class and its subclasses.
The public modifier allows properties and methods to be accessed from any context—both inside and outside the class.
class Person {
public $name;
public function sayHello() {
echo "Hello, my name is " . $this->name;
}
}
$person = new Person();
$person->name = "John";
$person->sayHello(); // Outputs: Hello, my name is John
Here, both $name and sayHello() are public, allowing direct access and modification from outside the class. While convenient, this can lead to unintended data changes if not used cautiously.
The private modifier restricts access to class members to within the class itself. It enforces strong encapsulation.
class Person {
private $name;
public function setName($name) {
$this->name = $name;
}
public function sayHello() {
echo "Hello, my name is " . $this->name;
}
}
$person = new Person();
$person->setName("John");
$person->sayHello(); // Outputs: Hello, my name is John
In this example, $name is private and cannot be accessed directly from outside. Instead, we expose a public setName() method that safely modifies the internal property.
The protected modifier is similar to private, but allows access from subclasses as well.
class Person {
protected $name;
public function setName($name) {
$this->name = $name;
}
public function sayHello() {
echo "Hello, my name is " . $this->name;
}
}
class Student extends Person {
public function study() {
echo $this->name . " is studying.";
}
}
$student = new Student();
$student->setName("John");
$student->sayHello(); // Outputs: Hello, my name is John
$student->study(); // Outputs: John is studying.
Here, the Student class inherits the Person class and has access to its protected $name property. This level of access is ideal for situations involving inheritance.
Using access modifiers properly allows you to protect the integrity of your class data and define clear boundaries for how objects interact. As best practices:
Prefer setting properties as private or protected.
Provide public methods to manipulate properties as needed.
Use protected when you expect subclass access.
Avoid exposing internal data directly with public properties unless necessary.
Mastering access control is a foundational step toward writing secure, maintainable, and professional object-oriented PHP code.