PHP Access Modifiers

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
?>

Try It Now

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
?>

Try It Now

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
?>

Try It Now

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
?>

Try It Now


🎯 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 private for sensitive data like passwords and bank details.
  • Use protected when you want child classes to access properties.

πŸ“ Practice Time!

Create a Superhero 🦸 class with different access modifiers. Test how they work with inherited classes!