0
0
PhpConceptBeginner · 3 min read

Access Modifiers in PHP: What They Are and How to Use Them

In PHP, access modifiers control the visibility of class properties and methods. The three main modifiers are public, protected, and private, which determine whether these members can be accessed from anywhere, only within the class and its children, or only inside the class itself.
⚙️

How It Works

Access modifiers in PHP work like different levels of privacy for parts of your code inside a class. Imagine a house where some rooms are open to all guests, some are only for family members, and some are locked and only the owner can enter. Similarly, public members are like open rooms anyone can enter, protected members are like family-only rooms, and private members are locked rooms only the owner (the class itself) can access.

This system helps keep your code organized and safe by controlling who can see or change important data or functions. It prevents accidental changes from outside the class and allows you to design clear rules for how your objects behave.

💻

Example

This example shows a class with all three access modifiers. It demonstrates which properties and methods can be accessed from outside the class.

php
<?php
class Car {
    public $brand;
    protected $engineNumber;
    private $ownerName;

    public function __construct($brand, $engineNumber, $ownerName) {
        $this->brand = $brand;
        $this->engineNumber = $engineNumber;
        $this->ownerName = $ownerName;
    }

    public function getOwnerName() {
        return $this->ownerName;
    }

    protected function getEngineNumber() {
        return $this->engineNumber;
    }
}

$myCar = new Car('Toyota', 'ENG12345', 'Alice');

// Accessing public property
echo $myCar->brand . "\n";

// Accessing protected property (will cause error if uncommented)
// echo $myCar->engineNumber . "\n";

// Accessing private property (will cause error if uncommented)
// echo $myCar->ownerName . "\n";

// Accessing private property via public method
echo $myCar->getOwnerName() . "\n";
?>
Output
Toyota Alice
🎯

When to Use

Use access modifiers to protect your class data and control how it is used. public is for things you want everyone to see or use, like a car's brand. protected is for details that only the class and its child classes should know, like an engine number. private is for sensitive information that should never be accessed outside the class, like the owner's name.

This helps avoid bugs and keeps your code easier to maintain. For example, if you want to change how the owner's name is stored, you can do it inside the class without breaking other parts of your program.

Key Points

  • Public members are accessible from anywhere.
  • Protected members are accessible only within the class and its subclasses.
  • Private members are accessible only within the class itself.
  • Access modifiers help encapsulate and protect data.
  • Use methods to safely access private or protected data.

Key Takeaways

Access modifiers control who can see or use class properties and methods in PHP.
Use public for open access, protected for limited access within class hierarchy, and private for strict internal use.
They help keep your code safe, organized, and easier to maintain.
Access private or protected data through public methods to control how it is used.