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.

Php Code
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
[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.

Php Code

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.

[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:

Php Code
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 analyze above section you can found that method test Parent() is not accessible from object of class. But it is accessible in child class.

Categorized in: