Access modifiers in PHP control the visibility of class properties and methods. PHP supports three access modifiers: public, protected, and private.
- They define where class properties and methods can be accessed.
- They help control access to class members and support encapsulation.
- They determine whether members are accessible within the class, in child classes, or from outside the class.
1. public Modifier
The public modifier allows properties and methods to be accessed from outside the class.
<?php
class Demo {
public $name = "GeeksforGeeks";
public function showName() {
return $this->name;
}
}
$obj = new Demo();
echo $obj->name . "\n";
echo $obj->showName();
?>
- $name is declared as a public property and can be accessed directly.
- showName() is a public method that returns the property's value.
2. private Modifier
The private modifier restricts access to the class that declares the property or method.
<?php
class MyClass {
private $secret = "Top Secret";
private function getSecret() {
return $this->secret;
}
public function revealSecret() {
return $this->getSecret();
}
}
$obj = new MyClass();
echo $obj->revealSecret();
?>
- $secret and getSecret() are private and cannot be accessed directly from outside the class.
- revealSecret() can access both because it is defined within the same class.
Attempting to access $obj->secret or call $obj->getSecret() from outside the class causes an access error.
3. protected Modifier
The protected modifier allows access within the declaring class and its child classes.
<?php
class ParentClass {
protected $message = "Hello from Parent";
protected function showMessage() {
return $this->message;
}
}
class ChildClass extends ParentClass {
public function getMessage() {
return $this->showMessage();
}
}
$obj = new ChildClass();
echo $obj->getMessage();
?>
Members Without an Explicit Access Modifier
In PHP, class methods are public by default if no visibility modifier is specified. However, properties must have a visibility modifier unless they use a supported property declaration mechanism, such as constructor property promotion.
The following example demonstrates a method without an explicit access modifier:
<?php
class Demo {
public $name = "GeeksforGeeks";
function showName() {
return $this->name;
}
}
$obj = new Demo();
echo $obj->name . "\n";
echo $obj->showName();
?>
- $name is explicitly declared as public.
- showName() has no visibility modifier, so it is public by default.
- Both can be accessed from outside the class.
public vs. protected vs. private
| public | protected | private |
|---|---|---|
| Accessible within the declaring class. | Accessible within the declaring class. | Accessible within the declaring class. |
| Accessible from child classes. | Accessible from child classes. | Not directly accessible from child classes. |
| Accessible from outside the class. | Not directly accessible from outside the class. | Not directly accessible from outside the class. |
Benefits of Using Access Modifiers
- Encapsulation: Controls access to an object's internal data and implementation.
- Data Protection: Prevents direct access to members that should remain restricted.
- Maintainability: Makes class interfaces easier to manage and update.
- Inheritance Control: Determines which members child classes can access.
Best Practices
- Use private for properties and methods that should remain internal to a class.
- Use protected when child classes need access to a member.
- Use public for methods and properties intended for external access.
- Prefer controlled public methods over exposing internal properties when validation or other business rules are required.

