Access Modifiers in PHP

PHP & MySQL Course: Access Modifiers in PHP

In previous lecture 10, we learnt about the important keywords and also about the abstract class. And we implement them in examples which become so easy for us to understand their concepts according to the OOP in PHP.

Lecture 11:Access Modifiers in PHP

In this lecture 11, we will learn about the modifiers in PHP. As we learnt modifiers in Java and C++ programming. The modifiers are used to access the data in different ways. Topics are:

  1. Private access modifier in PHP
  2. Public access modifier in PHP
  3. Protected access modifier in PHP

Private Access Modifier in PHP:

The private access modifier role in PHP is same as it is being used in Java and C++ OOP. We use the Private access Modifier in PHP to hide the data from the user. Its mean that the data will be used by the application and the data will be accessed inside the class or methods where they will be declare as private. Let’s check the Listing 1 about the Private Access Modifier in PHP

Public Access Modifier in PHP:

The public access modifier is used, the data is available for all as public. In this case, the data can be use anywhere. There is a example about the public access modifier in Listing 1.

Listing 1: Private & Public Access Modifier in PHP


<? class Labtop {

private $Model = "Dell Studio 1569";

private      $HardDrive = 500;

private              $Ram = 8;

public function Specification(){

return $this->Model;

}

}

class User extends Labtop{

public $UserName = "Ashley";

public $UserExperince = "Engineering";

public function UserInformation(){

echo $this->UserName." has a ";

echo $this->Specification();

}

}

$obj = new User;

echo $obj->UserInformation();

?>

Protect Access Modifier in PHP:

The Protected access Modifier is used the middle use of Public and Private. For example if we want to give the limited permission to the user to use the data. So we commonly use the Protected access modifier in PHP during the development of the web pages. Let’s check the Listing 2 for details about the protected access modifier. When we use the Protected in class so its mean that the data will be able to access inside the class and its child class too.

Listing 2: Protected Access Modifier in PHP


<?

class Labtop {

protected $Name = "Dell Studio";

public function NameDisplay1(){

echo "Accessing inside the Parent Class: ".$this->Name;

}

}

class Display extends Labtop{

public function NameDisplay2(){

echo "<br>Accessing inside the child Class: ".$this->Name;

}

}

$obj = new Labtop;

echo $obj->NameDisplay1();

$obj = new Display;

echo $obj->NameDisplay2();

?> 

Description:

In the listing 1 and 2 as you can see the usages of the access modifiers in PHP. In the listing 1 there are two access modifiers has been used. Public and Private, and these are fully understandable due to their name. The data which are public they can access from anywhere and the private data can be access just inside the class or where they declared once.

And in the listing 2, the protected access modifier in php is used. The data in which the protected access modifier is used they can just access inside their class inside the child class.

Summary:

These access modifiers in php are used to secure the data. And we also use them to make the access of the data from the use as limited and unlimited. These access modifiers are being used according to the data.

admin
 

student of computer Science , having interest and experience in C++,Java,Php,Html,Css,JavaScript, wordpress and android development