In this article, we will learn about class properties access modifier.All the properties and methods used in the classes with the keyword public. This means, that they are publicly available, or in any property of the script: within the class itself, inside and outside of classes that inherit.
But this flexibility may be too misleading and in some cases harmful, not to mention that the only bad public does not allow as per several Design Patterns Due to this reason, there are two other modifiers for visibility: protected and private .
Let’s see the descriptions of all the access modifier available in PHP.
public : the property or method declared as a public is respectively accessible / editable and callable from within the class itself, from inside and outside the class of classes that inherit.
protected : the property or method declared as protected is respectively accessible / editable and callable from within the class itself and from within the classes that inherit, but not accessible from outside the class.
private : the property or method declared as private is respectively accessible / editable and only be called from within the class itself.
Now let’s see some practical examples that will help us to understand the differences between public, protected and private that are obtained from the behavior of properties and methods which are declared with using specific modifiers.
Public
The visibility of public is the simplest and most immediate of the three. Declaring an element as public, you will have complete control on the same in every situation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | class MyClass { // Property public $a = 10; // Methods echo "Hello!"; } } class AnotherClass extends MyClass { // Property public $b = 10; } $myClass = new MyClass (); // Prints (10) echo $myClass->a; // Change $myClass->a = 20; // Call ("Hello!") $myClass->sayHello(); $anotherClass = new AnotherClass(); // Prints (10) $anotherClass->a; // Call ("Hello!") $anotherClass->sayHello(); |
We recall also that the constants declared within a class are always considered public and, therefore, accessible from anywhere.
Protected
The visibility of protected is more restrictive than the public. The properties and methods are not accessible and editable from outside the class but accessible from the classes that inherit it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | class MyClass { // Property protected $a = 10; // Methods protected function sayHello() { echo "Hello!" ; } } class AnotherClass extends MyClass { public function sayHelloAgain () { $this->sayHello(); } } $anotherClass = new AnotherClass(); // Print "Hello!" $anotherClass->sayHelloAgain(); |
If you try to access or modify a property protected or to call a method protected from outside the class:
1 2 3 4 5 6 |
All result in a Fatal Error :
Fatal error: Can not access protected property AnotherClass::$a in […] on line 116
Fatal error: Call to protected method MyClass::sayHello () from context ” in […] on line 116
Private
The visibility of private is the most restrictive than the above two public and protected. Properties declared as private can be accessed / modified only within the class that declares them, as well as private methods, can be called only on inside the class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | class MyClass { // Property private $a = 10; // Methods private function sayHello() { echo "Hello!"; } public function getAll() { echo $this->a; $this->sayHello(); } } class AnotherClass extends MyClass { public function sayHelloAgain() { $this->sayHello(); } } $anotherClass = new AnotherClass(); // Print "Hello 10" $anotherClass->getAll(); |
Conclusion
So the final conclusion from above explanations is to use public modifier or method if you want to access it from outside. To use Protected modifier when you want to access it only by classes that inherit and private is very restricted, accessible inside the own class only.
Access modifier is the concept of OOP PHP for making the application more robust and more maintainable.
Hope this article solve your confusion about access modifiers in PHP OOP.If you have any comment, let me know in comment section.
Comments (1)