Visibility in PHP Classes – onlinecode

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 :

  1. class demoClass
  2. {
  3. public $variable1;
  4. public $variable2;
  5. public functiondemoMethod()
  6. {
  7. }
  8. }
  9. $obj1=newdemoClass();
  10. echo$obj1->variable1;//accessible from outside
  11. $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 demoClass
See also  SPA Authentication using Laravel 10 Sanctum, Vue 3 and Vite

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 :

  1. Class test
  2. {
  3. public $variable1;
  4. private $variable2;
  5. public functionpubMethod($a)
  6. {
  7. echo$a;
  8. }
  9. private functionprivMethod($b)
  10. {
  11. echo$b;
  12. }
  13. public functionpubPrivMethod()
  14. {
  15. $this->variable2 =1;
  16. $this->privMethod(1);
  17. }
  18. }
  19. $objT=newtest();
  20. $objT->variable1 =3;//Works fine due to public visibility
  21. $objT->variable2 =1;//Throw fatal error of visibility because variable is set to private
  22. $objT->pubMethod("test");//give output : "test"
  23. $objT->privMethod(1);//Fatal error of visibility because method is set to private
  24. $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.
See also  Laravel 5.2 CRUD (Create Read Update Delete) Example from Scratch

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 :

  1. class parent
  2. {
  3. protected $var1;
  4. public $var2
  5. protected functiondemoParent()
  6. {
  7. echo this is demo;
  8. }
  9. }
  10. class child extends parent
  11. {
  12. public functiondemoChild()
  13. {
  14. $this->demoParent();//will work because it
  15. }
  16. }
  17. $objParent=newparent();
  18. $objParent->demoParent();//Throw error
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
See also  Inheritance in PHP OOP - onlinecode

Note: Protected is used in case of inheritance and interface.

Label :

PHP

OOP

Object Oriented Programming

Hope this code and post will helped you for implement Visibility in PHP Classes – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

  +  73  =  78

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US