Content · Web Development · PHP

Understanding public, protected, and private

Visibility keywords control which code can access properties and methods. Visibility alone is not authorization: users must still be checked before sensitive.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

Visibility keywords control which code can access properties and methods.

What It Does


Visibility keywords control which code can access properties and methods. The following examples distinguish a normal use case, a boundary case and a practical application.

Requirements and Compatibility


PHP 8.x and a local test environment. Apply input validation, correct error handling and the requirements of any optional libraries.

The code focuses on the core concept. Production implementation may also need routes, validation, persistence, authorization or deployment tooling.

Example 1: Basic Behavior


Start with the smallest meaningful working case.

<?php
class Account { public string $name = 'Ava'; protected int $level = 2; private string $token = 'internal'; public function level(): int { return $this->level; } }
$a = new Account(); echo $a->name . ':' . $a->level();

Example 2: Another Case


Examine the edge case and the observable result.

<?php
class BaseJob { protected function prefix(): string { return 'job'; } }
class EmailJob extends BaseJob { public function label(): string { return $this->prefix() . '-mail'; } }
echo (new EmailJob())->label();

Practical Example: Putting It to Use


Apply the technique to a small practical task.

<?php
final class Counter { private int $count = 0; public function add(): void { $this->count++; } public function value(): int { return $this->count; } }
$c = new Counter(); $c->add(); echo $c->value();

Important Behavior and Edge Cases


Visibility alone is not authorization: users must still be checked before sensitive actions.

Comparison with Related Functions or Approaches


OptionWhat it means in practice
Understanding public, protected, and privateVisibility keywords control which code can access properties and methods.
Related approachEncapsulation combines private state with public validated methods.

Common Beginner Mistakes


MistakeCorrect approach
Ignoring the important boundary for Understanding public, protected, and privateVisibility alone is not authorization: users must still be checked before sensitive actions.
Assuming a related mechanism has the same behaviorEncapsulation combines private state with public validated methods.

Key Takeaways


  • Visibility keywords control which code can access properties and methods.
  • Visibility alone is not authorization: users must still be checked before sensitive actions.
  • Encapsulation combines private state with public validated methods.

Before adapting these examples, verify the return values, test the failure paths and confirm that your application meets its security and environment requirements.

Share this resourceSend this page to someone who may find it useful.