AI Reading
Quick summary of this article
Access modifiers in PHP control how class properties and methods can be accessed. There are three levels: public (accessible from anywhere), protected (accessible only within the class and its child classes), and private (accessible only within the class itself). Using a Wi-Fi network analogy, the examples show how these modifiers protect sensitive information like passwords and IP addresses from outside access while still allowing controlled internal operations.
- Public members can be accessed freely from both inside and outside the class, making them suitable for general-use data like a network name.
- Protected members are restricted to the class where they are defined and any classes that inherit from it, but they cannot be called directly from outside the class.
- Private members are completely hidden from outside access and can only be used within the class where they are defined, ideal for sensitive data like IP addresses.
- Public methods can internally call protected or private methods, allowing a class to expose certain functionality while keeping other operations restricted.
- Attempting to access protected or private members from outside the class results in an error, ensuring strict control over how class data is used.
Access modifiers control the visibility and accessibility of class properties and methods in PHP.
There are three types of access modifiers:
public: The property or method can be accessed from anywhere (inside or outside the class).
protected: The property or method can only be accessed within the class itself or by classes that inherit from it.
private: The property or method can only be accessed within the class in which it is defined.
Example 1: Wi-Fi Access Levels
<?php
class WiFi {
public $networkName; // Public property
protected $password; // Protected property
private $ipAddress; // Private property
public function setNetworkName($name) {
$this->networkName = $name;
}
protected function setPassword($password) {
$this->password = $password;
}
private function setIpAddress($ip) {
$this->ipAddress = $ip;
}
}
$publicWiFi = new WiFi();
$publicWiFi->setNetworkName("AirportWiFi"); // OK
$publicWiFi->setPassword("airport123"); // ERROR
$publicWiFi->setIpAddress("192.168.1.1"); // ERROR
?>
Example 2: Using Access Modifiers in Methods
<?php
class WiFi {
public $networkName; // Public property
public function setPublicWiFi($name) {
$this->networkName = $name;
}
protected function setOfficeWiFi($name) {
echo "Setting up Office Wi-Fi: $name (Protected access)";
}
private function setPersonalWiFi($name) {
echo "Setting up Personal Mobile Wi-Fi: $name (Private access)";
}
public function setup() {
// Public method can access protected and private methods internally
$this->setOfficeWiFi("OfficeWiFi");
echo "<br>";
$this->setPersonalWiFi("PersonalWiFi");
}
}
$wifi = new WiFi();
$wifi->setPublicWiFi("AirportWiFi"); // OK
$wifi->setup(); // OK - internally calls protected and private methods
$wifi->setOfficeWiFi("OfficeWiFi"); // ERROR
$wifi->setPersonalWiFi("PersonalWiFi"); // ERROR
?>
Explanation of Examples:
Wi-Fi Access Levels (Properties):
The networkName property is public and accessible from anywhere.
The password property is protected and cannot be accessed directly from outside the class.
The ipAddress property is private and is completely hidden from outside the class.
Access Modifiers in Methods:
The setPublicWiFi method is public and can be called directly.
The setOfficeWiFi method is protected, so it can only be accessed by the class itself or derived classes.
The setPersonalWiFi method is private and can only be accessed from within the class.
These examples illustrate how access modifiers can be used to control and restrict access to class members in PHP.
