Thứ Tư, 15 tháng 7, 2015

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

Không có nhận xét nào:

Đăng nhận xét

Học lập trình web căn bản với PHP

Bài 1: Các kiến thức căn bản Part 1:  https://jimmyvan88.blogspot.com/2012/05/can-ban-lap-trinh-web-voi-php-bai-1-cac.html Part 2:  https://...