Visibility: public, private, protected
So far every property has been public: any code in the program could read and change it. For a visit counter that is handy; for a bank account it is dangerous. If the property $balance is open, any line can write $acc->balance = -500, and the account goes negative past every check.
A house has different doors too. Any guest may walk into the hall, but only the residents go into the bedroom. A guest may ask the host to bring something, but does not wander around the rooms.
In a class the guests are the code outside the class, and the residents are the class's own methods. Let's close the balance to guests.
private closes a property
<?php
class Account
{
public function __construct(private int $balance) {}
public function getBalance(): int
{
return $this->balance;
}
}
$acc = new Account(50);
echo $acc->getBalance(), "\n"; // 50Let's go through it step by step:
private int $balancein the constructor declares a promoted property closed by the wordprivate.- The method
getBalance()is written inside the class, so it sees$this->balanceand returns it. - The code outside asks for the balance through an open method, and
echoprints50.
The words public and private are visibility modifiers. A visibility modifier is the word before a property or method that says which code may reach it. PHP has three of them.
public
The property or method can be reached from anywhere in the program. This is how you declare what the rest of the code openly uses an object for: methods like getBalance().
private
Only code inside this class may reach it. This is how you close the data the class looks after itself. The modifier also goes before a method: a private method can be called only from other methods of the class.
protected
For code outside, protected closes things the same way private does. The difference between them shows up only when one class is built on top of another.
Getters and setters
The method getBalance() is a getter. A getter is an open method that hands out the value of a closed property. A setter is an open method that changes a closed property and checks the new value while doing so.
A setter for the account could be called setBalance(int $amount) and refuse a negative amount. Then the class itself makes sure the balance never goes negative, and nobody outside can get around that check.
Since PHP 8.4 reading and writing can be split even without a getter: the property public private(set) int $balance can be read by any code but changed only by the class itself.
Interviewers ask: "Why close properties when you could leave them open?" The answer:
- the class checks every change itself and keeps the object from getting into a wrong state;
- outside code depends only on the open methods, so the inside of the class can change without touching that code.
Closed to code, not to an object
Here the house picture is off. private closes a property to code outside the class, not to other objects. A method of a class can read the closed properties of any object of that class, not only its own:
<?php
class Wallet
{
public function __construct(private int $cash) {}
public function hasMoreThan(Wallet $other): bool
{
return $this->cash > $other->cash;
}
}
$mine = new Wallet(30);
var_dump($mine->hasMoreThan(new Wallet(20))); // bool(true)The method hasMoreThan is written inside Wallet, so it reads both $this->cash and $other->cash. In an interview, the answer "private is visible only inside its own object" counts as a mistake.
Common mistake
A closed property is read from outside the class:
echo $acc->balance, "\n"; // ✗Fatal error: Uncaught Error: Cannot access private property Account::$balanceThis is not a warning but an error: the lines above it have run, and the program stops at this one. Writing $acc->balance = 100; from outside ends with the same error. From outside, a closed property is reached only through open methods: $acc->getBalance().
Key points
privatecloses a property or method to code outside the class, andpublicopens it to everyone.- From outside, a closed property is read and changed through open methods—a getter and a setter.
privateis checked by class: a method of a class sees the closed properties of any object of that class.- Reading or writing a
privateproperty from outside is anError, and the program stops.