Home > DeveloperSection > Articles > Visibility in PHP

Visibility in PHP


PHP PHP 
Ratings:
0 Comment(s)
 200  View(s)
Rate this:

 Visibility in PHP


In this article we will focus how to deal with Visibility in PHP. Visibility is a big part in OOP. The concept of visibility is the scope of class members. It allows control over the class members from where it can be accessed, so that certain variables can be prevented from outside the class. Public, private and protected are three types of visibility in PHP classes which are available to control access of your class variable and property. By default, the visibility is public which means class variables and properties can be accessed from anywhere.

note.jpg Note-Visibility is mentioned in PHP 5. In PHP 4, declaration of a class variable, where you prefix variable name with “var”. This is an old way and should not be used anymore.

 

The visibility keywords include public, private and protected often written as PPP.

Public Visibility in PHP Classes

Public visibility is least restricted available in PHP. If you don’t define the visibility then by default it is public which means visibility is optional. Public visibility means that the variable/function can be used from anywhere and can be accessed globally. It can be accessed from object inside the class, or outside the class, or in child class. There is no limitation for public variables and public functions. Following is an example of public visibility in PHP:

<?php

class text

{

public $m;

public $n;

public function abc()

{

echo "Hello world!!"; // It will display Hello world!!

}

}

$obj= new text();

echo $obj->m;//accessible from outside

$obj->abc();//public method of the class text

?>

 

As in the above example test is the class. In this class everything is open.  Limited restriction in the class to access property and method using object outside the class.

Private Visibility in PHP Classes

Private visibility is used to hide data members and member function from outside world. If a property or method is declared as private then it is accessible only within the class who defines them.  Also these property and method cannot be accessible to the child class. Private visibility encapsulates the property or method of a class. Following is an example of private visibility in PHP:

<?php

class employee {

          private $fname;

 

          public function Name($fname) {

                   $this->name = $fname;

          }

 

          public function gName() {

                   return $this->name;

          }

}

 

$obj = new employee();

$obj->Name("Neha Singh");

echo $obj->gName(); //  the methods of the class have access to the private data members or methods

          

?>

 

Protected Visibility in PHP Classes

A Protected visibility is mainly used with inheritance. The member function or member variable of a class is accessible with in class and its derived class (or child class) but not outside the class. We can say that protected data function is public for the class that declares it and its derived class but not accessible (private) for the rest of the program (or outside world.)

Protected visibility is just like private visibility the only difference between them is that in protected child class can access protected data members of its parent class but in private the child class cannot access the private data members or function.

Following is an example of protected visibility in PHP:

<?php

class First

{

    protected $text = 'text';

 

    protected function term()

    {

       

        return $this->text; //This will be accessed as it is within the scope.

    }

}

 

class second extends First

{

    public function child()

    {

       

        return $this->text ."</br>". $this->term(); // Child class can access the protected data members of child class.

    }

}

 

$obj = new second();

 

 

echo $obj->child();

 

 

?>


Don't want to miss updates? Please click the below button!

Follow MindStick