Deceptively, the word visibility has nothing to do with application security! Instead it is simply a mechanism to control the use of your code. It can be used to steer an inexperienced developer away from the public use of methods that should only be called inside the class definition.
Indicate the visibility level by prepending the
public
,protected
, orprivate
keyword in front of any property or method definition. You can label properties asprotected
orprivate
to enforce access only through publicgetters
andsetters
.In this example, a
Base
class is defined with a protected property$id
. In order to access this property, thegetId()
andsetId()
public methods are defined. The protected methodgenerateRandId()
can be used internally, and is inherited in theCustomer
child class. This method cannot be called directly outside of class definitions. Note the use of the new PHP 7random_bytes()
function to create a random ID.class Base { protected $id; ...