Visibility public, private, protected
In PHP programming, object-oriented programming (OOP) concepts include class data type visibility, class properties, and class methods, where multiple class parameters are declared to control and manage data type behavior. Here, the programmer defines where a class property or class method can be accessed and controlled within the class, in public, private, and protected order.

In PHP programming, there are three main types of user-defined class visibility.
- public class parameters.
- private class parameters.
- protected class parameters.
Here, programmers can declare class data type variable parameters with keywords, according to their programming needs, such that the class data type parameters in a user-defined class can be accessed and controlled by a property or class method internally or externally within the class, or in a subclass (derived class) inheritance format, as needed.
So, let’s learn more about the public, private, or protected class keywords in PHP programming.
Public class keyword.
In a user-defined class program, the public keyword allows properties and class methods declared in parameter order to be accessed and controlled from anywhere in the class. Here, the programmer can access and control the public declared class element from both internal and external order in the class.
If the programmer wants a user-declared class program to have direct access and control by other existing classes or external inherited class code, then they use or declare it with the public parameter.
Public Visibility example in a class.
<?php
class employee {
// here Public class property declares
public $emp_name;
//declare public class method here
public function emp_info($emp_name) {
$this->emp_name = $emp_name;
}
public function display_info() {
echo “Employee Info – ” . $this->emp_name . “\n”;
}
}
// here we Create an object of the employee class
$employee1 = new employee();
// here we are accessing public property directly
$employee1->emp_name = “Siddhi”;
echo $employee1->emp_name . “\n”; //Result – Siddhi
// Calling a public method
$employee1->emp_info(“Engineer”);
$employee1->display_info(); // Result – Engineer
?>
Explanation of the Public Visibility class.
- In this program, the $emp_name class property and the emp_info() and display_info() class methods are public declared methods. This means that these class methods and parameters can be directly accessed externally within the class.
Private class keyword.
In a PHP program, the private keyword is used to declare class parameter properties and class methods, element variable methods, in order to access and control them only from within the class where they are defined. Remember, a class parameter object declared with the private class keyword cannot be directly accessed externally or in any inherited or subclass.
When programmers want to use the concept of encapsulating class data within a user-defined class, and prevent any external program code from directly accessing or modifying those class parameters, they can use the private class keyword as needed.
Example of private class visibility.
<?php
class employee {
//declare private class property method here
private $emp_name;
// here Public class method to set private class property
public function emp_detail($emp_name) {
$this->emp_name = $emp_name;
}
// here Public class method to access private property
public function emp_info() {
return $this->emp_name;
}
}
// here we Create an object of the employee class
$employee1 = new employee();
// here we try to attempt access private class property directly will produce as error
// echo $employee1->emp_name; // Display Error – we unable to access private property
// here we Access private class property via public class methods
$employee1->emp_detail(“Siddhi Deora”);
echo $employee1->emp_info(); // Result – Siddhi Deora
?>
Explanation of private class visibility.
- In this program, the $emp_name parameter property declared in the class is private in nature, so it cannot be directly accessed externally within the class.
- To define or retrieve the private property value declared in the class, we use the emp_detail() and emp_info() function methods in the public class method.
Protected class keyword.
In a PHP program, user-declared protected class properties and class methods can be accessed within the same class and through the class declaring it in the inheritance order (i.e., a class within a class). Remember, you cannot directly access protected class members by passing externally declared class parameters to a class.
When programmers want to grant permission to access or modify subclass properties or class methods, but want to keep them hidden from external program code, you can use protected class methods within that class.
Example of protected class visibility.
<?php
class employee {
// here declares the protected class property
protected $emp_name;
// here the public class method to set the protected property method
public function emp_detail($emp_name) {
$this->emp_name = $emp_name;
}
}
// here we Create a subclass (inherits from employee class)
class emp_department extends employee {
public function emp_info() {
echo “Employee Detail – ” . $this->emp_name . “\n”;
}
}
// here we Create an object of the employee subclass
$employee1 = new emp_department();
$employee1->emp_detail(“Siddhi Deora”); // here we try to Access protected class property via method in the parent or root class
$employee1->emp_info(); // Result – Employee Detail – Siddhi Deora
?>
Explanation of protected class visibility.
- In this program, the $emp_name class property is a protected class parameter in the employee declaration class. This allows programmers to directly access and control it within the class and through the emp_department subclass.
- Here, emp_department can directly access the $emp_name protected class property because it is inherited from the main employee class.
Main difference between public, private, or protected class keyword.
| Class Visibility | Class keyword Access Level | Usability in main or root class |
| Public class keyword | Public class keyword parameter can be Fully accessible across the active class | We can use or access public class data type Anywhere inside and outside of the class member |
| Private class keyword | Private class keyword parameter can be Restricted to the class itself of the class | Private class keyword declare class member can be access or controlled Only inside the class where the property/method is defined |
| Protected class keyword | Protected class keyword member data type can be Accessible within the class and inherit derived from subclasses | Protected class keyword declares class member usable or accessible to the Inside the class and by inherited classes (subclasses) |
A summary of public, private, and protected classes.
- In PHP programming, programmers can use the public class keyword to gain full access to class properties and methods, which they can freely access and control from anywhere within the class.
- In user-defined classes, declare parameters with the private class keyword to protect sensitive class data or program logic. This prevents external programs from directly accessing or modifying the class’s source code.
- If programmers want to provide access to inherited subclass properties or class methods while keeping the program source code hidden from external code, they can declare the class parameter data type variable with the protected keyword.

