PHP Access Modifiers


Introduction to Access Modifiers in PHP

Access modifiers (or visibility keywords) in PHP define the scope of properties and methods within classes and objects. They are essential for encapsulation, a core principle of Object-Oriented Programming (OOP).

PHP has three primary access modifiers:

  • public – Accessible from anywhere (inside/outside class, inherited classes)
  • protected – Accessible within the class and inherited (child) classes
  • private – Accessible only within the defining class (not inherited)


1. Public Access Modifier

Most permissive visibility level.

Can be accessed:

  • Inside the same class
  • Inside child (inherited) classes
  • Outside the class (via object instances)
<?php
class Car {
    public $model = "Tesla"; // Public property

    public function displayModel() { // Public method
        echo $this->model;
    }
}

$myCar = new Car();
echo $myCar->model; //  Works (public)
$myCar->displayModel(); //  Works (public)
?>

Output:

Tesla
Tesla


2. Protected Access Modifier

Accessible only within the class and its child (inherited) classes.

Cannot be accessed directly from outside the class.

<?php
class Vehicle {
    protected $brand = "Toyota"; // Protected property

    protected function getBrand() { // Protected method
        return $this->brand;
    }
}

class Car extends Vehicle {
    public function displayBrand() {
        echo $this->getBrand(); //  Works (child class access)
    }
}

$myCar = new Car();
$myCar->displayBrand(); //  Outputs "Toyota"
// echo $myCar->brand; ❌ Error (cannot access protected outside)
?>

Output:

Toyota


3. Private Access Modifier

Most restrictive visibility.

Accessible only within the same class (not even in child classes).

<?php
class User {
    private $password = "secret123"; // Private property

    private function showPassword() { // Private method
        return $this->password;
    }

    public function getPassword() {
        return $this->showPassword(); //  Works (same class)
    }
}

$user = new User();
echo $user->getPassword(); //  Outputs "secret123"
// echo $user->password; ❌ Error (private access)
?>

Output:

secret123


Access Modifiers in Inheritance

Modifier Same Class Child Class Outside Class
public Yes Yes Yes
protected Yes Yes ❌ No
private Yes ❌ No ❌ No


Best Practices for Access Modifiers

  • Use private by default (encapsulate data, prevent unwanted access).
  • Use protected when properties/methods need inheritance.
  • Use public only for APIs (methods meant to be called externally).
  • Avoid exposing internal logic (use getters/setters instead of public properties).


Example (Encapsulation with Getters/Setters):

<?php
class Product {
    private $price;

    public function setPrice($price) {
        if ($price > 0) {
            $this->price = $price;
        }
    }

    public function getPrice() {
        return $this->price;
    }
}

$product = new Product();
$product->setPrice(100);
echo $product->getPrice(); //  100
// $product->price = -50; ❌ Error (private)
?>

Output:

100