Public, Private, Protected in PHP: Why Without Them Your Code is Chaos?
\n\nImagine 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\nIf 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.
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\nWhat are Access Modifiers and Why Are They Needed?
\n\nAccess 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\nIn 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
Let's break down each one with practical examples.
\n\nPublic: "Enter Without Knocking"
\n\nThe 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).
<?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\nSeems 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.
When to use: for methods that are part of the class's public API (e.g., getUserName()), or for constants.
Private: "A Safe That Can't Be Cracked"
\n\nThe 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.
<?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\nIn 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