PHP Access Controls modifiers : Public, Private and Protected

The PHP access modifiers are used to set access rights with PHP classes and their members (functions and variables) defined within the class scope. In php there are three type of access control modifiers :

  • public
  • private
  • protected

1. Public : 

In a class the functions and variables defined with public access modifier are publicly accessible from anywhere, even from outside the scope of the class. In php public is the default access modifier.

Example :
<?php

class Student {
  public $name;
  public $age;

  function setData($arg1, $arg2) {
    $this->name = $arg1;
    $this->age = $arg2;
  }

  public function getName() {
    return $this->name;
  }

  public function getAge() {
    return $this->age;
  }

}

$stud = new Student;
$stud->setData("John", 15);

echo "Name : " . $stud->name . "<br/>";
echo "Age : " . $stud->age . "<br/><br/>";

echo $stud->getName() . ":" . $stud->getAge();

?>
Output :

Name : John
Age : 15

John:15

At the above example the class member variables are declared by using the public keyword, so we can access them directly by using the -> operator ( $stud->name ) from outside of the class. Similarly the member functions getName() and getAge are defined with public.


2. private : 

In a class the functions and variables defined with private access modifier are accessed only within the class itself.  It protects members from outside class access with the reference of the class instance, where outside the class means in inherited class.

Example :
<?php

class Student {
  private $name;
  private $age;

  function setData($arg1, $arg2) {
    $this->name = $arg1;
    $this->age = $arg2;
  }

  private function getName() {
    return $this->name;
  }

  private function getAge() {
    return $this->age;
  }

  function showData() {
    echo "Name : " . $this->getName() . "<br/>";
    echo "Age : " . $this->getAge();
  }

}

$stud = new Student;
$stud->setData("John", 15);

$stud->showData();

?>
Output :

Name : John
Age : 15

At the above example the member variables are defined with private access midifie, so we can not access it directly, outside of the class. And also the member functions getName() and getAge() are defined with private. So we can print the data by using the public function showData(), who calls the private functions getName() and getAge().


3. Protected :

The protected access modifier is same as private, except that it allows the subclasses or child classes to access protected superclass members. We can see more about subclass in PHP inheritance. 

Example :
<?php

class Student {
  protected $name;
  protected $age;

  function setData($arg1, $arg2) {
    $this->name = $arg1;
    $this->age = $arg2;
  }

  function showData() {
    echo "Name : " . $this->name . "<br/>";
    echo "Age : " . $this->age . "<br/><br/>";
  }

}

class child extends Student {
  function message() {
    echo "Student Name is " . $this->name . " and he is " . $this->age . " years old.";
  }
}

$stud = new child;
$stud->setData("John", 15);

$stud->showData();
$stud->message();

?>
Output :

Name : John
Age : 15

Student Name is John and he is 15 years old.


Next Topic :