Change language

PHP Encapsulation

Thus, the concept of OOP "Encapsulation in PHP" means including the internal details of an object to protect it from external sources. It describes by combining class, data variables, and member functions that work on data together in a single module to form an object. Otherwise, it is a combination of properties and behavior into one unit of the class.The data is not accessed directly, in fact, it is available through functions (GET or SET) written inside the class. The attributes remain private, but the getter (GET) and setter (SET) methods remain open to manipulate these attributes.PHP program to encapsulate:methods or functions in the following program update the password and check the title of the course. The GFG class defines all the operations associated with GFG users.  
// PHP program for encapsulation class GFG { private $userId ; private $pwd ; // Update GFG password public function updatePwd ( $userId , $pwd ) { // Write function body echo ( "Function to update password’ " . $pwd . " ’for user" . $userId ); echo "
"
; } // Check account balance public function courseName ( $userId ) { // Write function body echo ( "Function to check course name of user " . $userId ); echo "
"
; } } $obj = new GFG(); $obj -> updatePwd ( ’GFG12’ , ’ engineer54321’ ); $obj -> courseName ( ’GFG06’ );  
?>
Output:
Function to update password ’engineer54321’ for user GFG12 Function to check course name of user GFG06 
Note:Class data members and properties are not available to an external end user. Therefore, they cannot change the properties.Variable Access Program class Student { private $firstname ; private $gender ; public function getFirstName() { return $this -> firstname; } public function setFirstName ( $firstname ) { $this -> firstname = $firstname ; echo ( "First name is set to" . $firstname ); echo ( "
"
); } public function getGender() { return $this -> gender; } public function setGender ( $gender ) { if ( ’ Male’ ! == $gender and ’Female’ ! == $gender ) { echo ( ’Set gender as Male or Female for gender’ ); } $this -> gender = $gender ; echo ( "Gender is set to" . $gender ); echo ( "
"
); } } $student = new Student(); $student -> setFirstName ( ’Meena’ ); $student -> setGender ( ’Female’ );  
?>
Output:
First name is set to Meena Gender is set to Female 
Notes :
  • Encapsulation can be used if object properties are private and updated with public methods.
  • Encapsulation in PHP can be achieved by implementing access specifiers.
  • He is very careful with the concept of OOP inheritance, as inheritance can undermine the concept of encapsulation many times.
  • Inheritance exposes some of the details of the parent class, effectively breaking encapsulation.
Benefits of encapsulation:
  • Data hiding and abstraction:unnecessary details, internal representation and implementation are hidden from end users to protect data structure and class. Data access is denied to members of other classes by creating private methods. It protects the internal state of any object by keeping the member variables private and preventing any inconsistent state. It is an attachment of data and related operations to this object. Note:Encapsulation is used to hide internal representations from the client.
  • Data security: encapsulation helps make the data very robust and secure because the data and member functions are combined into one object. All tasks are done internally without any external worries, and this also makes life very easy.
  • Reduces complexity:Encapsulation helps reduce the complexity of software development by hiding implementation details and revealing methods or operations.
  • Reusable:There are instances, you don’t need to rewrite the same functionality that you inherited from the parent class.
  • Reliability :You can make a class read-only or write-only by writing SET or GET methods.
  • Easier code testing:PHP encapsulated code is easy to test because the functions used to test the child class also provide testing of the functions of the parent class.
  • Increased flexibility:class variables can be accessed using GET or SET methods, which increases flexibility. It is easy to maintain since the internal implementation can be changed without changing the code.
Conclusion:Object oriented programming in PHP is achieved through the concept of encapsulation, which is used to hide information. This reduces the readiness of the attributes of a given class. Get and set methods are used to prevent external unwanted access. It also helps in validating new values ​​assigned to properties. 
In short, PHP encapsulation & # 8212; it is the process of hiding all the secret details of an object that do not really contribute much to the critical characteristics of a class.

Shop

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Best laptop for engineering student

$

Best laptop for development

$

Best laptop for Cricut Maker

$

Best laptop for hacking

$890

Best laptop for Machine Learning

$950

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method