PHP Object Oriented Programming Part-5( Visibility or Access Modifier in PHP)

Share on FacebookTweet about this on TwitterShare on Google+Share on LinkedInPin on PinterestPrint this pageEmail this to someone

What is Visibility or Access modifier in PHP?

ccessibility of any property, constant or method is determined by visibility. Visibility is usually declared before constant, property and method.

How many visibility or access modifier in PHP?

There are three types of visibility in PHP:

  • Public
  • Protected
  • Private

Now let’s try to understand each Visibility with example:

Public:

If we want to use any property, method and constant of any class within the class, in child class and outside of the class, then these constant, property and method have to be declared as public. Let’s get a better understanding by this example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php
class GrandFather
{
    public $name='Masud Alam';  // A public variable
    public function getName(){
      return $this->name;
    }
}
class Daddy extends GrandFather // Inherited class
{
    function displayGrandFaName()
    {
        return $this->name; // The public variable will be available to the inherited class
    }
}
$grandFa=new GrandFather;
echo $grandFa->getName(),"\n";
// Inherited class Daddy wants to know Grandpas Name
$daddy = new Daddy;
echo $daddy->displayGrandFaName(),"\n"; // Prints 'Masud Alam'
// Public variables can also be accessed outside of the class!
$outsiderWantstoKnowGrandpasName = new GrandFather;
echo $outsiderWantstoKnowGrandpasName->name; // Prints 'Masud Alam
?>

Explanation: Observe, as our GrandFather class’s $name property is public, we were able to access the $name property from the current class, child class and even outside of the class.

Protected:

If we want to use any property, method and constant of any class within the class and it’s child class. then these constant, property and method have to be declared as protected. Let’s get a better understanding by this example:

READ  How to Generate a Unique ID in PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php
class GrandFather
{
    protected $name='Masud Alam';  // A Protected variable
    public function getName(){
      return $this->name;
    }
}
class Daddy extends GrandFather // Inherited class
{
    function displayGrandFaName()
    {
        return $this->name; // The Protected Property will be available to the inherited class
    }
}
$grandFa=new GrandFather;
echo $grandFa->getName(),"\n"; //Prints 'Masud Alam'
// Inherited class Daddy wants to know Grandpas Name
$daddy = new Daddy;
echo $daddy->displayGrandFaName(),"\n"; // Prints 'Masud Alam'
// Protected Property can not be accessed outside of the class!
$outsiderWantstoKnowGrandpasName = new GrandFather;
echo $outsiderWantstoKnowGrandpasName->name; // // Results in a Fatal Error
?>

Explanation: Observe, as our GrandFather class’s $name property is protected, we were able to access the $name property within the current class and it’s child class. But we were not able to access the $name property outside of the class or outside of it’s child class. If we do we will see a fatal error.

Private :

If we want to use any property, method and constant of any class only within the class itself and not in it’s child class or anywhere else. then these constant, property and method have to be declared as private. Let’s get a better understanding by this example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?php
class GrandFather
{
    private $name='Masud Alam';  // A Protected variable
    public function getName(){
      return $this->name;
    }
}
class Daddy extends GrandFather // Inherited class
{
    function displayGrandFaName()
    {
        return $this->name; // The Private Property will not available to the inherited class and will show notice
    }
}
$grandFa=new GrandFather;
echo $grandFa->getName(),"\n"; //Prints 'Masud Alam'
echo $gradFa->name, "\n"; //Results in a Notice
// Inherited class Daddy wants to know Grandpas Name
$daddy = new Daddy;
echo $daddy->displayGrandFaName(),"\n"; // Results in a Notice
// Protected Property can not be accessed outside of the class!
$outsiderWantstoKnowGrandpasName = new GrandFather;
echo $outsiderWantstoKnowGrandpasName->name; // // Results in a Fatal Error
?>

Explanation: Observe, as our GrandFather class’s $name property is private, we were able to access the $name property only within the current class. But we were not able to access the $name property anywhere else. If we try to access it anywhere else we will see an error.

READ  PHP Image Upload Using jQuery and MySQL
Share on FacebookTweet about this on TwitterShare on Google+Share on LinkedInPin on PinterestPrint this pageEmail this to someone

Leave a Reply

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