PHP Access Modifiers (original) (raw)
Last Updated : 17 Apr, 2025
In object-oriented programming, access specifiers are also known as access modifiers. These specifiers control how and where the properties or methods of a class can be accessed, either from inside the class, from a subclass, or from outside the class. PHP supports three primary access specifiers:
- public
- protected
- private
**1. public
A public properties can be accessed from anywhere, from within the class, by inherited (child) classes, and from outside the class.. If a property is declared public, its value can be read or changed from anywhere in your script.
**Now, let us understand with the help of the example:
PHP `
name; } } $obj = new Demo(); echo $obj->name . "\n"; echo $obj->showName(); ?>`
Output
GeeksforGeeks GeeksforGeeks
**2. private
A private property or method is accessible only within the class that declares it. It is not accessible in child classes or from outside the class.
**Now, let us understand with the help of the example:
PHP `
secret; } public function revealSecret() { return $this->getSecret(); // Allowed internally } } $obj = new MyClass(); echo $obj->revealSecret(); // Works echo $obj->secret; // Error: Cannot access private property echo $obj->getSecret(); // Error: Cannot access private method ?>`
Output
Top Secret Fatal error: Uncaught Error: Cannot access private property MyClass::$secret in /home/guest/sandbox/Solution.php:16 Stack trace: #0 {main} thrown in /home/guest/sandbox/Solution.php on li...
**3. protected
A protected property or method can only be accessed within the class itself and by inheriting classes (subclasses). It is not accessible from outside the class.
**Now, let us understand with the help of the example:
PHP `
message; } } class ChildClass extends ParentClass { public function getMessage() { return $this->showMessage(); // Allowed via inheritance } } $obj = new ChildClass(); echo $obj->getMessage(); // Works echo $obj->message; // Error: Cannot access protected property ?>`
Output
Hello from Parent Fatal error: Uncaught Error: Cannot access protected property ChildClass::$message in /home/guest/sandbox/Solution.php:18 Stack trace: #0 {main} thrown in /home/guest/sandbox/Solut...
Access Specifier | Access from own class | Accessible from derived class | Accessible by Object |
---|---|---|---|
Private | Yes | No | No |
Protected | Yes | Yes | No |
Public | Yes | Yes | Yes |
Why Use Access Specifiers?
- **Encapsulation: It prevents the object’s internal details from being accidentally changed or accessed.
- **Security: Prevents unauthorized access or modification of data.
- **Maintainability: Allows controlled interaction with class members, making code easier to maintain and debug.
- **Inheritance Management: Gives flexibility in what a child class should or shouldn’t access.
Best Practices
- Use private for properties that should never be accessed or modified directly from outside the class.
- Use protected when you expect subclasses to need access but want to restrict outside interference.
- Use public only when you intend the member to be accessible and safe to expose.
Conclusion
Access specifiers are foundational in PHP’s OOP model. Proper use of public, protected, and private helps build secure, and well-structured code. Choosing the right access level enforces better design practices and enhances code reusability and maintainability.