Home > Article > Backend Development > Understanding Access Modifiers in PHP OOP: Public, Protected, and Private
In PHP Object-Oriented Programming (OOP), access modifiers control the visibility of class properties and methods. The primary access modifiers in PHP are public, protected, and private.
This article will walk you through the purpose and usage of these access modifiers and explain how to apply them effectively in PHP OOP.
class User { public $name = "John"; public function greet() { return "Hello, " . $this->name; } } $user = new User(); echo $user->greet(); // Output: Hello, John
In this example, both the property $name and the method greet() are public, allowing them to be accessed directly from outside the class.
class Person { protected $age = 30; protected function getAge() { return $this->age; } } class Employee extends Person { public function showAge() { return $this->getAge(); // Correct: Accesses protected method within a subclass } } $employee = new Employee(); echo $employee->showAge(); // Output: 30
In this example, getAge() is a protected method, which is accessible within the Employee class, a subclass of Person.
class Person { protected $age = 30; protected function getAge() { return $this->age; } } $person = new Person(); echo $person->getAge(); // Error: Cannot access protected method Person::getAge()
Error Message: Fatal error: Uncaught Error: Cannot access protected method Person::getAge()
In this case, trying to access the protected method getAge() directly from an instance of Person results in an error because protected methods are not accessible from outside the class.
class BankAccount { private $balance = 1000; private function getBalance() { return $this->balance; } public function showBalance() { return $this->getBalance(); // Correct: Accesses private method within the same class } } $account = new BankAccount(); echo $account->showBalance(); // Output: 1000
In this example, the getBalance() method is private, so it can only be accessed within the BankAccount class. The showBalance() method is public and can be used to indirectly access the private getBalance().
class BankAccount { private $balance = 1000; private function getBalance() { return $this->balance; } } $account = new BankAccount(); echo $account->getBalance(); // Error: Cannot access private method BankAccount::getBalance()
Error Message: Fatal error: Uncaught Error: Cannot access private method BankAccount::getBalance()
In this case, trying to access the private method getBalance() directly from an instance of BankAccount results in an error because private methods are not accessible from outside the class.
class BankAccount { private $balance = 1000; private function getBalance() { return $this->balance; } } class SavingsAccount extends BankAccount { public function showBalance() { return $this->getBalance(); // Error: Cannot access private method BankAccount::getBalance() } } $savings = new SavingsAccount(); echo $savings->showBalance();
Error Message: Fatal error: Uncaught Error: Cannot access private method BankAccount::getBalance()
Here, the private method getBalance() is inaccessible even to subclasses like SavingsAccount, demonstrating that private methods cannot be accessed outside their defining class.
Modifier | Inside Class | Derived Class | Outside Class | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Yes | Yes | Yes | ||||||||||||||||
Protected |
Yes | Yes | No | ||||||||||||||||
Private | Yes | No | No |
The above is the detailed content of Understanding Access Modifiers in PHP OOP: Public, Protected, and Private. For more information, please follow other related articles on the PHP Chinese website!