When it comes to object-oriented programming in PHP, access modifiers play a crucial role in how classes and their properties and methods can be accessed within the program. Access modifiers are essentially keywords defining the level of access provided to certain class elements. PHP has three different access modifiers: public, private, and protected. In this article, we will take an in-depth look at these access modifiers and their practical applications within PHP programming.
Public Access Modifiers
The public access modifier is the most commonly used access modifier in PHP. It allows properties and methods to be accessed from anywhere, both within and outside of the class. When you declare a property or method as public within a class, you are essentially stating that it can be accessed by any object with an instance of that class.
class Example {
public $name = "John";
public function getName() {
return $this->name;
}
}
$example = new Example();
echo $example->name; // Outputs John
echo $example->getName(); // Outputs John
In the above example, we have a class called Example that contains a public property called $name
and a public method called getName()
. Both of these elements can be accessed from outside of the class by creating an instance of the class and calling the elements on that instance.
Public access modifiers are generally used in cases where you want the element to be widely accessible across the program. For example, you might use public access modifiers for methods or properties that need to be accessed by multiple objects or classes.
Private Access Modifiers
The private access modifier restricts access to the properties and methods to only within the class in which they were defined. This means that you cannot access private elements from outside of the class, including from instances of the class that are created elsewhere in the program.
class Example {
private $name = "John";
private function getName() {
return $this->name;
}
}
$example = new Example();
echo $example->name; // Results in a fatal error
echo $example->getName(); // Results in a fatal error
In the above example, we have updated the Example class to use private access modifiers instead of public. This means that the property $name
and the method getName()
cannot be accessed from outside of the class. If we attempt to access either of these elements on an instance of the class, as we did in the previous example, we will receive a fatal error message.
Private access modifiers are typically used in cases where you want to protect certain elements of the class from being accessed or modified by other parts of the program. For example, you might use private access modifiers for properties or methods that need to be kept secure or that are used internally within the class.
Protected Access Modifiers
The protected access modifier is similar to private in that it restricts access to the properties and methods of a class. However, protected access modifiers are slightly less restrictive; they allow access to elements within the class and within any subclasses or inherited classes.
class Example {
protected $name = "John";
protected function getName() {
return $this->name;
}
}
class SubExample extends Example {
public function echoName() {
echo $this->name; // This works because SubExample inherits from Example
echo $this->getName(); // This works because SubExample inherits from Example
}
}
$subExample = new SubExample();
echo $subExample->name; // Results in a fatal error
echo $subExample->getName(); // Results in a fatal error
$subExample->echoName(); // Outputs "JohnJohn"
In the above example, we have created a new class called SubExample that inherits from Example. SubExample has a public method called echoName()
that attempts to echo the protected property $name
and the protected method getName()
. Because SubExample is a subclass of Example, it is able to access these protected elements. However, we are not able to access these elements directly on an instance of SubExample.
Protected access modifiers are typically used in cases where you want to restrict access to certain elements of a class, but still allow those elements to be accessed by subclasses or inherited classes.
Conclusion
Access modifiers play a critical role in how classes and their elements are accessed within a PHP program. The public access modifier is the most commonly used, as it allows elements to be accessed from anywhere within the program. Private access modifiers restrict access to only within the class in which they were defined, while protected access modifiers restrict access to the class and any subclasses or inherited classes. By understanding the differences between these access modifiers, you can better control how your PHP program interacts with its objects and classes.
📕 Related articles about PHP
- PHP File Handling
- PHP Form Handling: A Comprehensive Guide
- Why Should you use PHP packages?
- PHP Data Types
- Mastering PHP Constants: A Comprehensive Guide
- PHP Include: A Comprehensive Guide