Properties and methods
Using object-oriented programming (OOP) concepts in PHP programming, user-declared class objects can implement properties and methods, both popular class components, which help programmers define a user-defined custom class data type structure in PHP programming. Properties in a class are declared class variables that hold data related to a class object. They indicate the object’s position within the current class. Similarly, methods in a class are custom user-declared functions that define the behavior of the existing class object. These help users access and modify the properties of the declared class.

So, let’s explore class (OOP) properties and methods in PHP programming in detail.
Class Properties in PHP Programming
Properties in user-defined classes in PHP programs are variables that programmers use to store and process data or state information about a class object. Properties used in user-defined classes can be public, private, or protected, which helps control their visibility and data type access level within the current program.
Multiple Class Property Visibility.
- Public class properties – Public properties declared in an existing class can be accessed and controlled from anywhere within the class program; for example, they can be accessed and managed both internally and externally within the declaring class.
- Private class properties – Private properties declared in a class can only be accessed and controlled from within that class.
- Protected class properties – Protected properties declared in a class can be accessed and managed by the properties object from within the class and by classes (subclasses) that inherit from that class.
Example of multiple class properties in a PHP program.
<?php
// Employee name class created
class employee {
/ Emp_name variable defined as a Public property
public $emp_name;
/ Here the emp_id variable defined as a Private property (it can only be accessed within the class environment)
private $emp_id;
/ Here the salary variable defined as a Protected property (it can only be accessed within the class and its subclasses inheritance order)
protected $salary;
/ Here we manually create a Constructor to initialize the class object properties
public function __construct($emp_name, $emp_id, $salary) {
$this->emp_name = $emp_name;
$this->emp_id = $emp_id;
$this->salary = $salary;
}
}
// Here we create an object of the employee class
$employee1 = new employee(“Siddhi”, “S101”, 89700);
// Accessing public properties directly
echo $employee1->emp_name . “\n”; // Result – Siddhi
// Here if we try to access class private properties directly, it will cause an error
// echo $employee1->emp_id; // It displays an error – Cannot access private properties of the class
// Here if we access protected properties directly, it will cause an error
// echo $employee1->salary; // It displays an error – Cannot access protected properties
?>
Explanation of multiple class properties.
- In this class program, emp_name is a public class property variable, so the programmer can call or access it directly from outside the class at any time.
- Similarly, emp_id is a private class property, so it cannot be directly accessed or called from outside the class.
- While salary is a protected class property, so it cannot be directly called or accessed. However, the programmer can access and control it through inherited features in subclasses.
Methods (Functions) in PHP Programming
Methods declared in a class are user-defined functions within a class that demonstrate the behavior of a class object to a program. Methods defined in a class help manipulate class object properties, perform user-desired actions on the class, and return class values.
Like class properties, methods declared in a class can be public, private, or protected, which controls the nature of the existing class data type variables and how they can be accessed, called, or controlled in the current environment.
Example of class methods in PHP programming.
<?php
// Employee class created
class employee {
public $emp_name;
private $emp_id;
protected $salary;
//employee class Constructor created
public function __construct($emp_name, $emp_id, $salary) {
$this->emp_name = $emp_name;
$this->emp_id = $emp_id;
$this->salary = $salary;
}
// use class Public method to display employee class element details
public function employeeDetails() {
echo “employee info = “. $this->salary. , $this->emp_name . , $this->emp_id . “\n”;
}
// use Private class method it allow only accessible within the class member data
private function getemp_id() {
return $this->emp_id;
}
// use Protected class method, it allows to access within this class and inherit subclasses only
protected function getsalary() {
return $this->salary;
}
}
//we Create an object of the employee class
$employee1 = new employee(“Siddhi”, “S101”, 43898);
//here we are calling a public method
$employee1->employeeDetails(); // Result – employee info = 43898 Siddhi S101
// if we Try to call private class method, it display directly will be cause an error
// echo $employee->getemp_id(); // Display Error: disabled Call to private method
// if we Try to call protected class method, it display directly will cause an error
// echo $employee->getsalary(); // Display Error: Unable to Call a protected method
?>
Explanation of Class Methods (Functions).
- In the above program, employeeDetails() is a public class method that can be accessed and controlled externally within the class to display information about the Employee class.
- Similarly, gettemp_id() is a private class method, so it can only be accessed and controlled from within the class.
- The getsalary() function is a protected class method, so it cannot be accessed or controlled externally within the class. However, the programmer can use it within the class and its subclasses in an inherited format.
Accessing Class Properties and Methods in PHP Programming.
In PHP programming, public properties and class methods in a user-defined class can be directly accessed from outside the class.
Accessing Class Properties and Methods example.
<?php
$employee1 = new employee(“siddhi”, “s101”, 98899);
/ Here we access the public property directly in the class
echo $employee1->emp_name . “\n”; // Result – siddhi
// Here we use the call public method directly in the class
$employee1->employeeDetails(); // Result – employee siddhi s101 98899
?>
Access private and protected properties/methods in a PHP program class.
To access private or protected properties and declared methods in a user-defined class program, programmers can use getter and setter class methods in a class program, or enable root class properties in subclasses using inheritance features.
<?php
// here we declare class with private or protected nature
class employee {
private $emp_id;
public function __construct($emp_id) {
$this->emp_id = $emp_id;
}
// here we use Getter class method to access private class property
public function getemp_id() {
return $this->emp_id;
}
// here we use Setter class method to modify private class property
public function setemp_id($emp_id) {
$this->emp_id = $emp_id;
}
}
$employee1 = new employee(“S1001”);
// here we try to Access private class property with a public getter method
echo $employee1->getemp_id(); // Result – S1001
// Here we modify a private class property with a public setter method.
$employee1->setemp_id(“H1002”);
echo $employee1->getemp_id(); // Result – H1002
?>
Explanation of accessing private and protected properties/methods.
- Getter class method – This method allows access and control of a private class property in a controlled way within the current class.
- Setter class method – This method allows modification of a private class object property while providing validation and other logic within the current class.