Understand the difference between public, protected, and private in PHP. Learn how access modifiers control visibility in classes with real-world examples and best practices.
When working with object-oriented programming (OOP) in PHP, you'll often encounter three important keywords: public, protected, and private. These are known as access modifiers. They define the visibility of class properties and methods, helping you control how your code is accessed and used.
In this blog post, we'll explore what these keywords mean, how they work, and when to use themβwith clear, real-world code examples.
π§ What Are Access Modifiers in PHP?
Access modifiers are used in class definitions to restrict or allow access to class members (properties and methods). PHP provides three access levels:
public: Accessible everywhereprotected: Accessible within the class and its child classesprivate: Accessible only within the class itself
Letβs explore each of them in detail.
π public Keyword in PHP
The public modifier allows class members to be accessed from anywhereβinside the class, outside the class, and in child classes.
β Example:
class Demo {
public $name = "Dinesh";
public function greet() {
return "Hello, " . $this->name;
}
}
$obj = new Demo();
echo $obj->name; // Output: Dinesh
echo $obj->greet(); // Output: Hello, Dineshπ Use Case:
Use public when you want properties or methods to be accessible to all parts of your code.
π private Keyword in PHP
The private modifier restricts access to class members only within the class where they are defined. They cannot be accessed outside the class or even from child classes.
β Direct Access Example (Error):
class Demo {
private $name = "Dinesh";
}
$obj = new Demo();
echo $obj->name; // β Error: Cannot access private propertyβ Correct Usage with Getter Method:
class Demo {
private $name = "Dinesh";
public function getName() {
return $this->name;
}
}
$obj = new Demo();
echo $obj->getName(); // Output: Dineshπ Use Case:
Use private for sensitive data or helper methods that should not be exposed outside the class.
π‘οΈ protected Keyword in PHP
The protected modifier allows class members to be accessed within the same class and in any class that inherits from it, but not from outside.
β Direct Access Example (Error):
class Demo {
protected $name = "Dinesh";
}
$obj = new Demo();
echo $obj->name; // β Error: Cannot access protected propertyβ Accessing from a Child Class:
class Demo {
protected $name = "Dinesh";
protected function greet() {
return "Hello, " . $this->name;
}
}
class ChildDemo extends Demo {
public function getGreeting() {
return $this->greet(); // Allowed: Accessing protected method from child class
}
}
$obj = new ChildDemo();
echo $obj->getGreeting(); // Output: Hello, Dineshπ Use Case:
Use protected when you want to share functionality between a parent and its child classes, but not expose it publicly.
π Summary Table
| Modifier | Same Class | Child Class | Outside Class |
|---|---|---|---|
public | β | β | β |
protected | β | β | β |
private | β | β | β |
π When to Use Each Modifier?
| Access Modifier | Best Used For |
|---|---|
public | When you want full access from anywhere |
protected | When you want to restrict access to class hierarchy only |
private | When data or logic should be completely internal to a class |
β Real-World Example
Letβs say youβre building a User class. You might want the name to be publicly readable, but the password should never be accessed or modified directly:
class User {
public $username;
private $password;
public function __construct($username, $password) {
$this->username = $username;
$this->password = $password;
}
public function checkPassword($inputPassword) {
return $this->password === $inputPassword;
}
}
$user = new User("Dinesh", "secret123");
echo $user->username; // β
Public access
// echo $user->password; // β Error: Private property
echo $user->checkPassword("secret123"); // β
Trueπ― Final Thoughts
Understanding the differences between public, protected, and private is essential for writing clean, secure, and maintainable PHP code. They give you the tools to encapsulate data, prevent accidental modification, and build reusable class hierarchies.
π Use public sparingly, protected strategically, and private thoughtfully.
Happy Coding! π