Visibility in PHP Classes

In previous chapter we have discuss public, private and protected at various place. Public, private and protected are three type of visibility in php classes available for controlling access of your class variable and property. Everty visiblity level has it won rights. For example if your property or variable is available with public, means it can be use anywhere.

Available Visibility in PHP Classes

There are 3 type of visibility available in php for controlling your property or method.

  1. Public: Public method or variable can be accessible from anywher. I mean from inside the class, out side the class and in child(will dicuss in next chapter) class also.
  2. 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.
  3. 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 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

Next >> Static Method and Property in OOP

Comments

  1. Sankar V says

    $objChild->setChild();//work because test child will call test parent.

    Please check the code. $objChild->setChild(); replace to $objChild->testChild();

  2. ajdeguzman says

    you can’t use “parent” as class name because it’s a keyword. but I understand because it’s for the purpose of demo. Great tuts :D

  3. rr says

    Thanks for this article but this article and many other articles on this website use very error filled English. Please write proper English so that tutorials on this site are very clear without the reader having to scratch his head. Otherwise the tutorials are good.

Leave a Reply

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


three + three =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>