PHP Access Modifiers
Controlling property and method visibility
🔒 What are Access Modifiers?
Access modifiers control where properties and methods can be accessed from. PHP has three access modifiers: public (accessible everywhere), private (only within the class), and protected (within class and child classes).
<?php
class BankAccount {
public $accountNumber; // Accessible everywhere
private $balance; // Only within this class
protected $owner; // Within this class and child classes
public function __construct($number, $balance, $owner) {
$this->accountNumber = $number;
$this->balance = $balance;
$this->owner = $owner;
}
public function getBalance() {
return $this->balance; // Can access private property inside class
}
}
$account = new BankAccount("12345", 1000, "John");
echo $account->accountNumber; // Works: public
echo $account->getBalance(); // Works: public method
// echo $account->balance; // Error: private property
?>
Output:
12345
1000
Three Access Modifiers
Public
Accessible from anywhere in the code
public $name;
public function getName() {}
Private
Only accessible within the same class
private $password;
private function validate() {}
Protected
Accessible in class and child classes
protected $data;
protected function process() {}
Encapsulation
Hide internal implementation details
private $secret;
public function getSecret() {}
🔹 Public Access Modifier
Public properties and methods can be accessed from anywhere: inside the class, outside the class, and in child classes. This is the most permissive access level.
<?php
class Product {
public $name;
public $price;
public function __construct($name, $price) {
$this->name = $name;
$this->price = $price;
}
public function getInfo() {
return "$this->name costs $$this->price";
}
}
$product = new Product("Laptop", 999);
echo $product->name; // Direct access: Laptop
echo $product->price; // Direct access: 999
echo $product->getInfo(); // Method access: Laptop costs $999
?>
Output:
Laptop
999
Laptop costs $999
🔹 Private Access Modifier
Private properties and methods can only be accessed from within the same class. They're hidden from outside code and child classes, providing strong encapsulation.
<?php
class User {
public $username;
private $password;
public function __construct($username, $password) {
$this->username = $username;
$this->password = $password;
}
public function verifyPassword($input) {
// Can access private property inside class
return $this->password === $input;
}
private function hashPassword($pass) {
return md5($pass); // Private method
}
}
$user = new User("john", "secret123");
echo $user->username; // Works: john
echo $user->verifyPassword("secret123"); // Works: 1 (true)
// echo $user->password; // Error: Cannot access private
// $user->hashPassword("test"); // Error: Cannot call private method
?>
Output:
john
1
🔹 Protected Access Modifier
Protected properties and methods can be accessed within the class and by child classes that inherit from it, but not from outside code.
<?php
class Animal {
protected $species;
protected $sound;
public function __construct($species, $sound) {
$this->species = $species;
$this->sound = $sound;
}
protected function makeSound() {
return $this->sound;
}
}
class Dog extends Animal {
public function bark() {
// Can access protected properties and methods from parent
return "$this->species says: " . $this->makeSound();
}
}
$dog = new Dog("Dog", "Woof");
echo $dog->bark(); // Works: Dog says: Woof
// echo $dog->species; // Error: Cannot access protected
// echo $dog->makeSound(); // Error: Cannot call protected method
?>
Output:
Dog says: Woof
🔹 Comparing Access Modifiers
This example demonstrates all three access modifiers in action, showing where each can and cannot be accessed.
<?php
class Example {
public $publicVar = "Public";
private $privateVar = "Private";
protected $protectedVar = "Protected";
public function showAll() {
// All accessible inside the class
echo "Inside class:
";
echo $this->publicVar . "
";
echo $this->privateVar . "
";
echo $this->protectedVar . "
";
}
}
class ChildExample extends Example {
public function showInherited() {
echo "In child class:
";
echo $this->publicVar . "
"; // Works
echo $this->protectedVar . "
"; // Works
// echo $this->privateVar . "
"; // Error: private not inherited
}
}
$obj = new Example();
$obj->showAll();
echo "
";
$child = new ChildExample();
$child->showInherited();
echo "
";
echo "Outside class:
";
echo $obj->publicVar . "
"; // Works
// echo $obj->privateVar; // Error
// echo $obj->protectedVar; // Error
?>
Output:
Inside class:
Public
Private
Protected
In child class:
Public
Protected
Outside class:
Public
🔹 Practical Example: Encapsulation
Access modifiers enable encapsulation, protecting sensitive data while providing controlled access through public methods (getters and setters).
<?php
class Employee {
private $name;
private $salary;
protected $department;
public function __construct($name, $salary, $department) {
$this->name = $name;
$this->setSalary($salary);
$this->department = $department;
}
// Getter methods (public)
public function getName() {
return $this->name;
}
public function getSalary() {
return "$" . number_format($this->salary, 2);
}
// Setter with validation (public)
public function setSalary($salary) {
if ($salary > 0) {
$this->salary = $salary;
}
}
// Private helper method
private function calculateBonus() {
return $this->salary * 0.1;
}
public function getBonus() {
return "$" . number_format($this->calculateBonus(), 2);
}
}
$emp = new Employee("Alice", 50000, "IT");
echo "Name: " . $emp->getName() . "
";
echo "Salary: " . $emp->getSalary() . "
";
echo "Bonus: " . $emp->getBonus() . "
";
$emp->setSalary(55000);
echo "New Salary: " . $emp->getSalary();
?>
Output:
Name: Alice
Salary: $50,000.00
Bonus: $5,000.00
New Salary: $55,000.00