PHP public, protected and private on shop coupons, plus readonly and 8.4.
In one line
Access modifiers sort properties, methods and constants by who may see them. PHP has three: public (everyone), protected (the class and its children) and private (the class only). A method with no keyword is public.
Why you need it
If the rate and secret of a coupon are all public, order page code can set the rate to 90 and nothing stops it. Tracking down the line that did it takes ages.
Split what the world may see (code, apply()), what only children may touch (percent) and what the class keeps to itself (secret), and the blast radius of every change is known in advance. private names can be renamed without fear.
Everyday picture
Picture a shop. The shelves (public) are open to every customer, the stockroom (protected) to staff of the main store and its franchises, and the safe in the office (private) only to people of that one shop. Just as shop staff can open any safe in their own shop, PHP private lets code of a class open that slot on any object of the class.
Press Run to try your edited code right away. The step-through and the output above follow the original code.
PHP visibility at a glance
Same class
Child class
Outside code
Another object of the same class
public
O
O
O
O
protected
O
O
X
O
private
O
X
X
O
readonly is a separate flag on top of visibility: once set, the value cannot change (since 8.1). PHP 8.4 adds public private(set) to give reading and writing different scopes (new in 8.4). A method with no keyword is public.
When to use it
Start with everything private and open things one step at a time when needed
Use protected only for values children truly must change
For read-only access from outside, consider public readonly (8.1) or public private(set) in 8.4
PHP has no package-level visibility like Java. A namespace only groups names; it does not limit who can see them.
Common mistakes
Reading a parent private property in a child
To child code, a parent private slot simply does not exist. PHP does not stop; it warns and hands back null, so the bug surfaces late. If children need the value, make it protected.
A child must be callable wherever the parent is. Outside code calls $coupon->apply(), so a child that blocks it would break things, and PHP refuses the class when loading it.
Wrong
classCoupon{publicfunctionapply(int$price): int {return$price; }}classVipCouponextendsCoupon{protectedfunctionapply(int$price): int {return$price - 1000; }}
What PHP saysFatal error: Access level to VipCoupon::apply() must be public (as in class Coupon)
Fixed
classCoupon{publicfunctionapply(int$price): int {return$price; }}classVipCouponextendsCoupon{publicfunctionapply(int$price): int {return$price - 1000; }}echo (newVipCoupon())->apply(5000), "\n";
When you run it4000
Changing a readonly value inside the class
readonly is not only for outsiders; even the class itself may set it only once. When you need a changed value, return a new object from a with...() method.