Current Location: Home> Latest Articles> PHP Access Modifiers Explained: Mastering Public, Protected, Private, and Final

PHP Access Modifiers Explained: Mastering Public, Protected, Private, and Final

M66 2025-10-13

Overview of PHP Access Control

In web application development, access control is crucial for ensuring security. PHP offers several access modifiers to control the visibility of classes, properties, and methods. This article explains these modifiers in detail and provides examples of how to use them effectively.

Public Modifier

The public modifier is the most basic in PHP. It allows classes, properties, and methods to be accessed from anywhere. Members declared as public can be called from outside the class. For example:

class Car {
    public $color; // public property
    public function drive() { // public method
        echo "Driving";
    }
}

In the code above, the $color property and drive() method are public, so they can be accessed by any object:

$myCar = new Car();
$myCar->color = "red"; // Accessing public property
$myCar->drive(); // Calling public method

Protected Modifier

The protected modifier restricts access to the class itself and its subclasses. External code cannot access protected members directly. For example:

class Car {
    protected $color; // protected property
    protected function drive() { // protected method
        echo "Driving";
    }
}

Subclasses can access protected members from the parent class:

class SportsCar extends Car {
    public function set_color($color) {
        $this->color = $color; // Accessing protected property
    }
}

$sportsCar = new SportsCar();
$sportsCar->set_color("blue"); // Calling public method

Private Modifier

Private is the strictest access modifier. Members declared as private can only be accessed within the class itself. External code cannot access or call them. For example:

class Car {
    private $mileage; // private property
    private function update_mileage() { // private method
        $this->mileage++;
    }
}

Attempting to access private members outside the class will result in an error:

$myCar = new Car();
$myCar->mileage = 1000; // Error
$myCar->update_mileage(); // Error

Final Modifier

The final modifier can be applied to classes and methods, indicating they cannot be inherited or overridden. This ensures critical methods or classes remain unchanged. For example:

class Car {
    final public function drive() { // final method
        echo "Driving";
    }
}

class SportsCar extends Car {
    public function drive() { // This will cause an error
        echo "Driving fast";
    }
}

Using final ensures important methods or classes remain unmodified, enhancing code stability and security.

Summary

This article covered PHP's common access modifiers and their use cases:

  • public: allows access and calls from anywhere
  • protected: allows access and calls only within the class and subclasses
  • private: allows access and calls only within the class itself
  • final: prevents members from being modified by subclasses

Mastering these modifiers helps implement proper access control, better code structure, and improves the security and maintainability of applications.