PHP Access Modifiers
PHP Access Modifiers: Controlling Visibility with public, protected, and private
PHP access modifiers—public, protected, and private—control which parts of your codebase can read or call a class's properties, methods, and constants. Encapsulating internal state behind clear visibility boundaries prevents outside code from breaking object invariants.
Comparing Visibility Levels in PHP
Choose the strictest visibility level that still lets collaborators use your class cleanly.
| Modifier | Inside Same Class | Inside Child Classes | Outside the Class |
|---|---|---|---|
public |
Yes | Yes | Yes |
protected |
Yes | Yes | No |
private |
Yes | No | No |
<?php
declare(strict_types=1);
class Order
{
private array $items = [];
protected float $taxRate = 0.08;
public function addItem(string $name, float $price): void
{
if ($price < 0) {
throw new InvalidArgumentException('Price cannot be negative.');
}
$this->items[] = ['name' => $name, 'price' => $price];
}
public function calculateTotal(): float
{
$subtotal = array_sum(array_column($this->items, 'price'));
return round($subtotal * (1 + $this->taxRate), 2);
}
}
$order = new Order();
$order->addItem('USB Hub', 30.00);
echo "Total with tax: $" . $order->calculateTotal() . PHP_EOL;
Because $items is private, external callers must go through addItem(), which guarantees negative prices can never sneak into the order total.
Combining Visibility with readonly
In PHP 8.1+, marking a property public readonly string $id lets outside code read $order->id directly without writing a boilerplate getId() getter, while still blocking external reassignments.
Common Mistakes to Avoid
- Leaving properties
publicout of habit: Mutablepublicproperties let any caller bypass validation checks. Default toprivateorpublic readonly. - Making helper methods
publicwhen only used internally: Internal formatting or parsing helpers belong inprivatemethods so you can refactor them freely later without breaking callers. - Forgetting visibility keywords on methods: Omitting
public/privatedefaults a method topublic, though PSR-12 coding standards require declaring visibility explicitly on every method and property.
Frequently Asked Questions
Can two instances of the same class access each other's private properties?
Yes. In PHP, visibility is enforced at the class level, not the object instance level—so $this->compare($otherOrder) can read $otherOrder->items.
When should I choose protected instead of private?
Use protected only when a property or hook method is intentionally designed to be overridden or read by subclasses. Otherwise, prefer private.
Can constants also have access modifiers in PHP?
Yes. Since PHP 7.1, class constants support public const, protected const, and private const.
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Try it Yourself
Experiment with the code from this lesson in our interactive playground.