Surfe.be - passive income

How to Use Access Modifier in PHP

When we follow object oriented php in our project , we should follow some rules to use access modifiers in php. Today we are going to learn clearly what is access modifier and how can we use it.PHP access modifiers are used to set access rights with PHP classes and their members that are the functions and variables defined within the class scope. In php there are three scopes for class members.

  • public
  • private
  • protected

Now, let us have a look at the following image to understand access modifier access level

Now, let us have a look at the following list to know about the possible PHP keywords used as access modifiers.

public :- class or its members defined with this access modifier will be publicly accessible from anywhere, even from outside the scope of the class.

private :- class members with this keyword will be accessed within the class itself. we can’t access private data from subclass. It protects members from outside class access.

protected :- same as private, except by allowing subclasses to access protected superclass members.

Now see the table to understand access modifier

 

ACCESS MODIFIERS ACCESS LEVEL
class                                private           protected         public
parent class / same class              Yes               Yes               Yes
Subclass                               No                Yes               Yes
Outside class                          No                 No               Yes

 

Now we will see it from the source code. Let’s see

PUBLIC

We can access all the public data from any where. Parent class , subcluss or outside subclass doesn.t matter where from we are going to access data. To clearly understand see the below code

class Student  //Parent Clas
{
	
    public $age;

	public function __construct()
	{
		$this->age = '25';
	}

    public function ShowData()
    {
    	echo $this->age.'Parent Class'; //We can access from parent class / same class 
    }
}


  class Teacher extends Student {  //Subclass
  	
  	 public function ShowData()
    {
    	echo $this->age . 'Subclass'; //We can access from subclass
    }
  }


/* From outside class */

$object = new Student();
echo $object->age; //We can access from outside class

PRIVATE

We can access all the private data only from inside the parent class. subclass or outside class we can’t access data. To clearly understand see the below code

class Student
{
  
  private $age;

  public function __construct()
  {
    $this->age = '25';
  }

    public function ShowData()
    {
      echo $this->age.'Parent Class'; //We can access from parent class
    }
}


  class Teacher extends Student {
    
     public function ShowData()
    {
      echo $this->age . 'Subclass'; //We can't access from subclass class
    }
  }
  
/* From outside class */

$object = new Student();
echo $object->age; //We can't access from outside class
 

PROTECTED

We can access all the protected data both from parent class and subclass. Outside class we can’t access data. To clearly understand see the below code

class Student
{
  
  protected $age;

  public function __construct()
  {
    $this->age = '25';
  }

    public function ShowData()
    {
      echo $this->age.'Parent Class'; //We can access from parent class
    }
}


  class Teacher extends Student {
    
     public function ShowData()
    {
      echo $this->age . 'Subclass'; //We can access from subclass class
      echo $this->age . 'Subclass'; 
    }
  }
  
/* From outside class */

  $object = new Student();
  echo $object->age; //We can't access from outside class

Hope you will understand all these procedure. If you like this tutorial please leave a comments and share with your friends. If you find any error of this tutorial , please share this with me. You can read also ADVANCE PHP CRUD APPLICATION WITH PDO

Leave a comments

LET'S SOCIALITE

Recent Tweets

RECOMMENDED POST