Visibility in PHP Classes or Access modifiers in PHP

Access modifiers (or access specifiers) are keywords in object-oriented languages that set the accessibility of classes, methods, and other members.

There are three Visibility in PHP

  • Public
  • Private
  • Protected

Public :  Public variable or methods is accessible from anywhere means you can access them within or outside the class.

Private: Private variable or methods are only accessible within the class means you can’t access the private variables or class outside the class.

Protected: Protected variable or method are accessible in Drived class or you can say in child class. Protected is used in case of Inheritance.

 

Public Visibility in PHP Classes

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. Following is the example of the public visibility in php classes:
class test
{
public $abc;
public $xyz;
public function xyz()
{
}
}
$objA = new test();
echo $objA->abc;//accessible from outside
$objA->xyz();//public method of the class test

So in above example class test is the very basic class. In this class every thing is open. Mininum restriction in the class is to access its properyt and methods using object outside the class.

Private Visibility in PHP Classes

Private method or properties can only be accessible withing 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. Following example of Private visibility in php classes.

Class test
 {
 public $abc;
 private $xyz;
 public function pubDo($a)
 {
 echo $a;
 }
 private function privDo($b)
 {
 echo $b;
 }
 public function pubPrivDo()
 {
 $this->xyz = 1;
 $this->privDo(1);
 }
 }
 $objT = new test();
 $objT->abc = 3;//Works fine
 $objT->xyz = 1;//Throw fatal error of visibility
 $objT->pubDo("test");//Print "test"
 $objT->privDo(1);//Fatal error of visibility
 $objT->pubPrivDo();

//Within this method private function privDo and variable xyz is called using $this variable.

Protected Visibility in PHP Classes

Protected visibility in php classes are only useful in case of inheritance and interface. We will discuss in dept 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:
 class parent
 {
 protected $pr;
 public $a
 protected function testParent()
 {
 echo this is test;
 }
 }
 class child extends parent
 {
 public function testChild()
 {
 $this->testParent(); //will work because it
 }
 }
 $objParent = new parent();
 $objParent->testParent();//Throw error
 $objChild = new Child();
 $objChild->setChild();//work because test child will call test parent.
 If you will take anaylze above section you can found that method testParent() is not accessible from object of class. But it is accessible in child class.
Always use correct visibility in php classes to keep your structure healthy. Do not use code like this. It is break all visibility of your php class.
 class test
 {
 public function method($method)
 {
 $this->$method();
 }
 private function abc()
 {
 //Do Something
 }
 protected function xyz()
 {
 //do something
 }
 }
 $objT = new test();
 $objT->method('abc');
 $objT->method('xyz');

This is the very stupid implementation of public, private and protected. Because you can call any type of method in this implementation.

For the further detail about visibility in php classes you can go to:
http://php.net/manual/en/language.oop5.visibility.php

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

WordPress.com.

Up ↑

%d bloggers like this: