Visibility in PHP Classes :
You are ever noticed that every method has their visibility in php.There are 3 types of visibility which is used in php for managing your class methods.
1) Public Visibility in PHP OOP :
If we set any method as public then it means it can be accessible from anywhere either from inside the class, outside the class and in child class.There are no any limitation for public accessors .Example of public visibility in php oop is given below :- class demoClass
- {
- public $variable1;
- public $variable2;
- public function demoMethod()
- {
- }
- }
- $obj1 = new demoClass();
- echo $obj1->variable1;
- $obj1->demoMethod();
class demoClass{public $variable1;public $variable2;public function demoMethod(){}}$obj1 = new demoClass();echo $obj1->variable1;//accessible from outside$obj1->demoMethod();//public method of the class demoClass
So in above example you are notice that everything is open, there is no any limitation.Variable and method are accessible from outside the class.
2) Private Visibility in PHP OOP :
If we set any method as private then it means it can be accessible within that class only it can't accessible from outside of class or in child class.You can access property and method within class by using $this keyword.
private method or property in php are used to set restriction over class which means you don't want that outside of classes can access the method.
Example of private visibility in php oop is given below :- Class test
- {
- public $variable1;
- private $variable2;
- public function pubMethod($a)
- {
- echo $a;
- }
- private function privMethod($b)
- {
- echo $b;
- }
- public function pubPrivMethod()
- {
- $this->variable2 = 1;
- $this->privMethod(1);
- }
- }
- $objT = new test();
- $objT->variable1 = 3;
- $objT->variable2 = 1;
- $objT->pubMethod("test");
- $objT->privMethod(1);
- $objT->pubPrivMethod();
Class test{public $variable1;private $variable2;public function pubMethod($a){echo $a;}private function privMethod($b){echo $b;}public function pubPrivMethod(){$this->variable2 = 1;$this->privMethod(1);}}$objT = new test();$objT->variable1 = 3;//Works fine due to public visibility$objT->variable2 = 1;//Throw fatal error of visibility because variable is set to private$objT->pubMethod("test");//give output : "test"$objT->privMethod(1);//Fatal error of visibility because method is set to private$objT->pubPrivMethod();//Within this method private function privMethod and variable variable2 is called using $this variable.
3) Protected Visibility in PHP OOP :
If we set any method as protected then it means it can be accessible within the class itself or in child class both. Example of protected visibility in php oop is given below :- class parent
- {
- protected $var1;
- public $var2
- protected function demoParent()
- {
- echo this is demo;
- }
- }
- class child extends parent
- {
- public function demoChild()
- {
- $this->demoParent();
- }
- }
- $objParent = new parent();
- $objParent->demoParent();
class parent{protected $var1;public $var2protected function demoParent(){echo this is demo;}}class child extends parent{public function demoChild(){$this->demoParent(); //will work because it}}$objParent = new parent();$objParent->demoParent();//Throw error
Note: Protected is used in case of inheritance and interface.