Home > Backend Development > PHP Tutorial > Encapsulation in PHP

Encapsulation in PHP

WBOY
Release: 2024-08-29 13:01:19
Original
1317 people have browsed it

The following article, Encapsulation in PHP, provides a detailed outline for PHP encapsulation.

ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock Tests

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

  • In earlier days, the computer programs were a long sequence of commands. The commands were then clubbed into the list of commands called the functions.
  • All the data was stored in one location. It was modified from every location wherever it was called, which caused many problems as data modified in one function would affect the other functionality. So to overcome this issue, OOP uses Encapsulation in PHP. Let’s see what encapsulation is all about.
  • Encapsulation is a fundamental topic in Object-Oriented Programming Language (OOP). It states the concept of binding data (attributes) and methods (functions) into a single unit (Class). This term is also referred to as Information Hiding.
  • This is achieved by making data and methods of class private. It solves the problem at the implementation level rather than at the design level.
  • Encapsulation is specifically used for protection. It is the process of hiding the data of the object from other classes/methods, and access to the data is prohibited to the members of the class.
  • If you have an attribute that others cannot see and you bundle it with functions or methods that provide all the access to the data members, you can hide the content and control access to the object.
  • Users will be unaware of the inner implementation and will only get to know the upper or the top view of the functionality as to how the class is storing the data into variables. The functions of an object can access the functions of other objects.
  • In our day to day life, encapsulation is like our wardrobe where we can keep our shirts, pants, etc. In this case, the wardrobe will be the main class, and the attributes will be clothes, boxes, etc.

How Does Encapsulation Work in PHP?

  • As we all know, encapsulation is used to hide the details from users. To achieve this, we create the object of a class, and the object calls the methods and methods to perform their task with defined properties.
  • Let’s take an example of an Air Conditioner and Air Conditioner Manufacturer.
    Suppose you are an Air Conditioner Manufacturer and you have designed and integrated an Air Conditioner design (class), now; by using your equipment, you are manufacturing an Air Conditioner (object) for selling purpose; when you sell your Air Conditioner, the user needs to know how to use an Air Conditioner instead of that how it works OR let’s assume you have a laptop and there is an interface for you to interact with it, you will use the services of the laptop instead of knowing how it actually works.
  • This means that you are creating the class with functions. By making objects of the class, you are making the availability of your class’s functionality by that object and without the interference in the original class.
  • Encapsulation is just how you wanted your objects or variables to be visible in your implemented code.

Example

class employee { public $name; private $id; protected $tax; public $age; }
Copy after login

If you have to access private/protected properties, you can use getter and setter methods in your class.

As the name states,

  1. The getter method retrieves an attribute in other classes.
  2. The setter method modifies/changes the value of the attribute/data.
  • Depending on the methods that we implement, we can decide what permissions have to be given. We can give read-only, write-only, full permissions or no permissions at all. To achieve this, we use access specifiers such as public, private and protected.
  • You can use public property anywhere inside the class as well as outside the class, but you cannot use private and protected properties outside the class directly by calling it. Private methods can be accessed within the same class, and protected methods can be accessed only by the subclasses. Outside, the class can’t access the private or protected method of other classes.
  • Using getter and setter methods, you can access the variables and methods outside the class as well. Take a look at the employee class; in the class, add the following method.
class employee{ public function employee_ID(){ return $this->id; }}
Copy after login

And we can access it by calling the function like this.

$obj = new employee(); echo $obj->employeeID()
Copy after login

Example of Encapsulation in PHP

Following is an example:

Implemented Code:

<!DOCTYPE html>
<html>
<head>
<title> Encapsulation in PHP </title>
</head>
<body>
<?php
class Employee
{
public $employee_name;
public $employee_id;
function __construct($emp_name, $emp_id)
{
$this->employee_name = $emp_name;
$this->employee_id = $emp_id;
}
public function Employee_details()
{
echo "Employee name is $this->employee_name and Employee id is $this->employee_id";
}
}
$obj = new Employee("Abhishek", "123456");
echo $obj->Employee_details();
?>
</body>
</html>
Copy after login

Output:

Encapsulation in PHP

Benefits of Encapsulation in PHP

  • Encapsulation helps us in binding the data (variables/attributes) and the member functions of a class.
  • The fields of the class can be made read-only or write-only using access specifiers such as public, private and protected.
  • It provides your code more security, flexibility and easy to maintain.
  • Using Encapsulation, your methods and data attributes cannot be called directly; hence no illegal data access by users.
  • It allows us to modify the implemented code without breaking the logic/code of others who use the same code and functions.
  • It provides the integrity of an object’s data.
  • The user of the class doesn’t know how the class stores the data.
  • Encapsulation prevents unexpected changes in the data.
  • Encapsulation also improves re-usability, and it is easy to change with new requirements or needs.

Conclusion

In this article, we learned one of the most important object-oriented programming concepts. Encapsulation is one of the most famous and commonly used techniques to overcome the functions and methods that are called in multiple locations.

The importance of encapsulation is to hide the user’s complex code and show the real functionality to the user. If proper encapsulation is not followed in the code, it leads to its bad designing. Thus, it is reflected by the fact that most of the modern programming languages support encapsulation, such as PHP, JAVA, etc.

The above is the detailed content of Encapsulation in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template