Public, Private, Protected in PHP: A Guide to Encapsulation

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Public, Private, Protected in PHP: Why Without Them Your Code is Chaos?

\n\n

Imagine you are building a house. You have a front door (everyone can enter), a bedroom with a lock (only for family), and a safe in the wall (only you have access). In the world of object-oriented programming (OOP) in PHP, such "doors" and "locks" are called access modifiers.

\n\n

If you have ever written classes in PHP, you have come across the words public, private, and protected. But what do they actually mean? Why can't you just make everything public and live in peace? The answer is simple: without them, your code will turn into a public thoroughfare, where any script can accidentally break the program's logic.

\n\n

In this article, we will break down access modifiers in PHP with real examples, understand the difference between them, and learn how to apply them to create reliable, flexible code. This guide is based on a popular Stack Overflow question and is adapted for the educational website pythonlib.ru.

\n\n

What are Access Modifiers and Why Are They Needed?

\n\n

Access modifiers are keywords that determine from where a class's properties and methods can be accessed. They are the foundation of encapsulation (one of the four pillars of OOP). Encapsulation hides the internal workings of an object and protects data from incorrect use.

\n\n

In PHP, there are three levels of access:

\n\n
    \n
  • Public — accessible to everyone (both inside and outside the class).
  • \n
  • Private — accessible only within the class itself.
  • \n
  • Protected — accessible within the class and in child classes.
  • \n
\n\n

Let's break down each one with practical examples.

\n\n

Public: "Enter Without Knocking"

\n\n

The public modifier is the most open. Properties and methods declared as public can be read and modified from anywhere: from another class, from a global script, from outer space (just kidding).

\n\n
<?php\nclass Car {\n    public $brand = 'Toyota';\n\n    public function honk() {\n        return 'Beep!';\n    }\n}\n\n$myCar = new Car();\necho $myCar->brand; // Toyota — works\n$myCar->brand = 'Honda'; // Changing it — works\necho $myCar->honk(); // Beep!\n?>
\n\n

Seems convenient, right? But there is a problem. Imagine a User class has a public $password property. Any external code can read and change it. This is a security disaster.

\n\n

When to use: for methods that are part of the class's public API (e.g., getUserName()), or for constants.

\n\n

Private: "A Safe That Can't Be Cracked"

\n\n

The private modifier is the complete opposite of public. A property or method is accessible only within the class where it is declared. Even child classes cannot see the parent's private members.

\n\n
<?php\nclass BankAccount {\n    private $balance = 1000;\n\n    private function calculateInterest() {\n        return $this->balance * 0.05;\n    }\n\n    public function getBalance() {\n        return $this->balance;\n    }\n\n    public function applyInterest() {\n        $this->balance += $this->calculateInterest();\n    }\n}\n\n$account = new BankAccount();\n// echo $account->balance; // Error! Cannot access private property\n// echo $account->calculateInterest(); // Error! Cannot access private method\necho $account->getBalance(); // 1000 — works because the method is public\n$account->applyInterest();\necho $account->getBalance(); // 1050\n?>
\n\n

In this example, $balance and calculateInterest() are hidden from the outside world. We manage the balance only through the public methods getBalance() and applyInterest(). This is encapsulation

Blogs

Book Recommendations