Available Visibility in PHP Classes
There are 3 type of visibility available in php for controlling your property or method.
Public:
Public method or variable can be accessible from anywhere. I mean from inside the class, out side the class and in child class also.
Private:
Method or property with private visibility can only be accessible inside the class. You can not access private method or variable from outside of your class.
Protected:
Method or variable with protected visibility can only be access in the derived class. Or in other word in child class. Protected will be used in the process of inheritance.
Public:
Public visiblity is least restricted visibility available in php.
If you will not define the visibity factor with your method or property then public will be by defautl applied.
Public methods or variables can be accessible from anywhere
.For example, It can be accessible from using object(outside the class), or inside the class, or in child class.
[ad type=”banner”]Private:
Private method or properties can only be accessible within the class.
You can not access private variable or function of the class by making object out side the class.
But you can use private function and property within the class using $this object.
Private visibility in php classes is used when you do not want your property or function to be exposed outside the class.
example of Private visibility in php classes.
[ad type=”banner”]
Protected:
Protected visibility in php classes are only useful in case of inheritance and interface.
We will discuss in depth of interfaces and inheritance in other chapter of this tutorial.
Protected method or variable can be accessible either within class or child class. Here we will take very basic example:
If you will take analyze above section you can found that method test Parent() is not accessible from object of class. But it is accessible in child class.