Visibility in PHP Classes – onlinecode
In this post we will give you information about Visibility in PHP Classes – onlinecode. Hear we will give you detail about Visibility in PHP Classes – onlinecodeAnd how to use it also give you demo for it if it is necessary.
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 functiondemoMethod()
- {
- }
- }
- $obj1=newdemoClass();
- echo$obj1->variable1;//accessible from outside
- $obj1->demoMethod();//public method of the class demoClass
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 demoClassSo 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 functionpubMethod($a)
- {
- echo$a;
- }
- private functionprivMethod($b)
- {
- echo$b;
- }
- public functionpubPrivMethod()
- {
- $this->variable2 =1;
- $this->privMethod(1);
- }
- }
- $objT=newtest();
- $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.
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.