public private protected

Public Private Protected – PHP

Public Private Protected are also known as access control modifiers.

Public:

Public method or variable can be accessible from anywhere. we can access them inside the class, outside the class and in child class also.

Private

Private: Method or property with private visibility can only be accessible inside the class. we can not access private method or variable outside the class.

Protected:

Method or variable with protected visibility can only be accessed within a class and in the derived class. We use Protected in the process of inheritance.

Let’s see an example which will explain above all .

class GrandPa
{
    private $name='Pratik';
    public $sirname = "Patel";
    protected $gender = "Male";
    function getName()
    {
        echo $this->name;
    }
    function getGender()
    {
        echo $this->gender;
    }
}
class Daddy extends GrandPa // Inherited class
{
   function displayName()
   {
        return $this->name; // The private variable will not be available to the inherited class
   }
   function displaySirname()
   {
       echo $this->sirname; // The Public variable will be available to the inherited class and outside the class
   }
   function displayGender()
   {
       echo $this->gender; // The protected variable will be available to within a class and inherited class but not outside the class.
   }
}
$grandpa = new GrandPa;
$daddy = new Daddy;
?>

The privete variable within a class:
<?php
echo "The privete variable within a class.<br><br>";
echo $grandpa->getName();
?>

Output:

The privete variable within a class.
Pratik

The protected variable within a class:

<?php
echo "<br><br>The protected variable within a class.<br><br>";
echo $grandpa->getGender();
?>

Output:

The protected variable within a class.
Male

The private variable will not be available to the inherited class:

<?php
echo "<br><br>The private variable will not be available to the inherited class<br><br>";
echo $daddy->displayName();
?>

Output:

public private protected error

The public variable will be available to the inherited class:

<?php
echo "<br><br>The public variable will be available to the inherited class.<br><br>";
echo $daddy->sirname;
?>

Output:

The public variable will be available to the inherited class.
Patel

The public variable will be available to the inherited class and out side the class:

<?php
echo "<br><br>The public variable will be available to the inherited class and out side the class.<br><br>";
echo $daddy->displaySirname();
?>

Output:

The public variable will be available to the inherited class and out side the class.
Patel

The protected variable will be available to the inherited class:

<?php
echo "<br><br>The protected variable will be available to the inherited class.<br><br>";
echo $daddy->displayGender();
?>

Output:

The protected variable will be available to the inherited class.
Male

The protected variable will be available to the inherited class but not out side the extended class:

<?php
echo "<br><br>The protected variable will be available to the inherited class but not out side the extended class.<br><br>";
echo $daddy->displayGender();
?>

Output:

public private protected error

Hope, This article is useful to you and you enjoy a lot while reading it.

Leave a Reply