PHP Access Modifiers – Controlling Visibility π¦
In PHP, access modifiers define the visibility of class properties and methods. They control how data inside a class can be accessed.
PHP has three main access modifiers:
publicβ Accessible from anywhere (inside or outside the class).privateβ Accessible only inside the same class.protectedβ Accessible inside the same class and subclasses.
πΉ Public Access Modifier
A public property or method can be accessed from anywhere.
π Example 1: Using Public Properties
<?php
class Car {
public $brand = "Tesla"; // Public property
public function showBrand() {
return "Car Brand: " . $this->brand;
}
}
$myCar = new Car();
echo $myCar->showBrand(); // β
Works fine
?>
Since $brand is public, we can access it directly from outside the class.
πΉ Private Access Modifier
A private property or method can only be accessed inside the class.
π Example 2: Using Private Properties
<?php
class BankAccount {
private $balance = 1000; // Private property
public function showBalance() {
return "Your balance is $" . $this->balance;
}
}
$account = new BankAccount();
echo $account->showBalance(); // β
Works fine
// echo $account->balance; β Error: Cannot access private property
?>
The balance is private, so it cannot be accessed from outside the class. We can only use showBalance() to get the value.
πΉ Protected Access Modifier
A protected property or method can be accessed inside the class and by subclasses.
π Example 3: Using Protected Properties
<?php
class Animal {
protected $species = "Unknown"; // Protected property
protected function getSpecies() {
return "Species: " . $this->species;
}
}
class Dog extends Animal {
public function showSpecies() {
return $this->getSpecies(); // β
Can access protected method inside a subclass
}
}
$dog = new Dog();
echo $dog->showSpecies(); // β
Works fine
// echo $dog->species; β Error: Cannot access protected property
?>
The species property is protected, so it cannot be accessed directly from outside the class, but it can be used in a subclass.
π Example 4: Combining Public, Private, and Protected
<?php
class Person {
public $name; // Public - Can be accessed anywhere
private $age; // Private - Can only be accessed inside the class
protected $secret; // Protected - Can be accessed in subclasses
public function __construct($name, $age, $secret) {
$this->name = $name;
$this->age = $age;
$this->secret = $secret;
}
public function getAge() {
return "Age: " . $this->age; // β
Works fine
}
}
class Spy extends Person {
public function revealSecret() {
return "Secret: " . $this->secret; // β
Works fine (protected property)
}
}
$person1 = new Person("Alice", 25, "Loves pizza π");
echo $person1->name; // β
Works fine
// echo $person1->age; β Error: Cannot access private property
$spy = new Spy("Bob", 30, "Top Agent π΅οΈββοΈ");
echo $spy->revealSecret(); // β
Works fine
?>
π― Key Takeaways
publicβ Can be accessed from anywhere.privateβ Can only be accessed inside the class.protectedβ Can be accessed inside the class and its subclasses.- Use
privatefor sensitive data like passwords and bank details. - Use
protectedwhen you want child classes to access properties.
π Practice Time!
Create a Superhero π¦Έ class with different access modifiers. Test how they work with inherited classes!