Php Access Modifiers

PHP access modifiers are used to set access rights with PHP classes and their methods and variables.
There are 3 types of access modifiers:
(i) private
(ii) protected
(iii) public

Modifier Name With in Class Out Side Class Inheritance
Private

allow

disallow

disallow

Protected

allow

disallow

allow

Public

allow

allow

allow

private access modifiers

class variable or method with private access modifier can only be accessible inside the class. You can not access private method or variable from outside of your class.

Note:- Class can not be private.

Example:- In the below example we are accessing $stu_name variable and student_college_name method outside the class then show fatal error and if we are accessing both in the class then its working fine.


<?php 
class student {
	private $stu_name = 'John';
	private function student_college_name() {
		return 'HBIT Kanpur';
	}

	public function display_college_name() {
		return $this -> stu_name . ' is in ' . $this -> student_college_name();
	}

}

$stu_obj = new student;
echo $stu_obj -> stu_name; //Output:- Fatal error: Cannot access private property student::$stu_name
echo $stu_obj -> student_college_name(); //Output:- Fatal error: Call to private method student::student_college_name()
echo $stu_obj -> display_college_name(); //Output:- John is in HBIT Kanpur
?>

protected access modifiers

Class variable or method, that are set to be protected, can only be accessed inside the class and by its subclasses, in other words its class and sub classes.

Note:- Class can not be protected.

Example:- In the below example we are accessing $stu_name variable and student_college_name method outside the class then show fatal error and if we are accessing in the child class then its working fine.


<?php 
class student {
	protected $stu_name = 'John';
	protected function student_college_name() {
		return 'HBIT Kanpur';
	}

	
}
class student_information extends student
{
public function display_college_name() {
		return $this->stu_name . ' is in ' . $this->student_college_name();
}	
	
}
$stu_obj = new student;
$stuinfo_obj = new student_information;
echo $stu_obj -> stu_name; //Output:- Fatal error: Cannot access protected property student::$stu_name
echo $stu_obj -> student_college_name(); //Output:- Fatal error: Call to protected method student::student_college_name()
echo $stuinfo_obj -> display_college_name(); //Output:- John is in HBIT Kanpur
?>

public access modifiers

The public access modifier is accessible everywhere. If you define your class properties and methods as “public”, then it can be used anywhere in your PHP script.
Example:- In the below example we call the variable $stu_name and method student_college_name() out side of the class, then it is working fine.


<?php 
class student {
	public $stu_name = 'John';
	public function student_college_name() {
		return 'HBIT Kanpur';
	}
}
$stu_obj = new student;
echo $stu_obj -> stu_name; //Output:- John
echo $stu_obj -> student_college_name(); //Output:- HBIT Kanpur
?>

Php Access Modifiers – Interview Questions

Q 1: What are access modifiers?

Ans: They control access to class properties and methods.

Q 2: Types of access modifiers?

Ans: public, private, protected.

Q 3: What is public access?

Ans: Accessible from anywhere.

Q 4: What is private access?

Ans: Accessible only within the class.

Q 5: What is protected access?

Ans: Accessible within class and subclasses.

Php Access Modifiers – Objective Questions (MCQs)

Q1. Which access modifier allows access only within the class?






Q2. Which access modifier allows access in the class and its subclasses?






Q3. Which access modifier allows access from anywhere?






Q4. Private properties cannot be accessed ______.






Q5. Access modifiers help in ______.






Related Php Access Modifiers Topics