Overview
Access modifiers are keywords that determine the accessibility of properties and methods within classes. Access modifiers are also called visibility modifiers.
PHP has three access modifiers:
publicprivateprotected
Public
Properties or methods set to public are accessible inside and outside the class in which they are declared.
Look carefully at the following code listing, which demonstrates the internal and external use of properties and methods.
The logic:
- A phone's sales price is calculated by multiplying the base price with a price multiplier.
- The multiplier is supposed to be retrieved from elsewhere (configuration file, value in a database, ...) but we're setting it to a hardcoded value for simplicity.
Internal use
The $base_price and $sales_price are accessed internally by the calculateSalesPrice() method.
The $multiplier property is accessed internally by the calculatePriceMultiplier() and calculateSalesPrice() methods.
The calculatePriceMultiplier() method is used inside the class, in the calculateSalesPrice() method.
External use
The following are accessed from outside the class:
$base_price(when we set the price to 100)calculateSalesPrice()$sales_price(when we print the price)
We are able to access these properties and methods externally because their accessibility is set to public. We would not be able to do so if they were set to private.
Private
Properties or methods set to private are ONLY accessible INSIDE class. They can NOT be accessed from the OUTSIDE.
Let's say your class declares a private $serial_number property:
- inside the class, methods are able to access it with
$this->serial_number - outside the class, accessing it like
$myobject->serial_numberis NOT POSSIBLE
Why use private properties / methods?
There are times when you need certain properties and methods to provide everything the class needs, but you don't want to expose those to the people using your class.
Example: private method
Let's say that our phone only works if we pay a monthly subscription. Before each interaction with the phone, the device needs to check if the subscription is active.
This functionality is important to the internal working (the business logic) of the class, but we don't want to expose any part of how it works to the user. We don't even want to tell the user that they can't perform the operation because of their inactive subscription; we only want to show a generic error message.
However, when you try to execute $myphone->subscriptionIsActive(), you get an error:
Uncaught Error: Call to private method Phone::subscriptionIsActive()This was to be expected, as `subscriptionIsActive()` is a private method, and private methods are not accessible / visible outside a class definition.
Example: private property
Let's say our phones have an internal system that tracks how many calls were placed in total and how many of those failed to connect.
This information is only intended for the manufacturer's Quality Assurance process and should not be accessed by end users:
In the above example, the $total_calls and $failed_calls are private because the creator of the class does not want to expose this information to the users of the class.
The getConnectionFromCellTower() method is also private, because end users should not try to make cell tower connections on their own; this should only happen as part of the process of making a phone call, for which the user has access to the public makePhoneCall() method.
Protected
Using the protected keyword only makes sense when you are using inheritance, a technique where one class can inherit things from another class.
Inheritance is an important part of Object Oriented Programming, and will be discussed in detail later, together with the protected keyword.
Summary
- Access modifiers are keywords that determine the accessibility of properties and methods within classes.
- Access modifiers are also called visibility modifiers.
- PHP has 3 Access Modifiers: public, private, and protected.
- Public properties or methods are accessible inside or outside of their class.
- Private properties or methods are only accessible from within their class.
- Protected properties or methods are only accessible inside their class or inside child classes. This is part of the inheritance mechanism and will be discussed later.