Home » encapsulation in php

encapsulation in php

Today, We want to share with you encapsulation in php.In this post we will show you polymorphism in php, hear for data abstraction and encapsulation in php we will give you demo and example for implement.In this post, we will learn about Concept of Encapsulation in PHP OOP Example with an example.

php encapsulation best practices

In this tutorial we learn to all about encapsulation in php Examples like as a Program Explanation, encapsulation and polymorphism, data abstraction or many more.

Explain Encapsulation in PHP

Let’ understand this through an example.

<?php
   class PROJECT {
      private $memberid;
      private $mobilecode;
      public function MobileUpdate($memberid,$mobilecode) {
               ---------execute tasks-----
               }
      public function CheckAuthReport($memberid,$mobilecode){
               ---------execute tasks-----
               }
      public function miniStatement($memberid) {
               ---------execute tasks-----
               }
      }
   $obj = new PROJECT();
   $obj ->CheckAuthReport(9*******9,1**3);
?>

PHP encapsulation Program Explanation:

In this PHP example, all the PROJECT class data members (variable) are marked with the private modifier. It implies that i can not straight access PROJECT class data members (property). So, i can’t update the class property straight. The only approach to update the class property (data members) is calling a function (function). That’s the issue i have stated all the PROJECT class methods with a public access modifier. The client can pass the expected arguments to a class function to execute a each task.

Read Also:  How to Insert Data in Database using PHP OOPS

Suppose anyone wants to check balance then he reqireds to access the CheckAuthReport() function with the required arguments memberid=”9*******9″andmobilecode=”9**8″. This is called Data hiding through Encapsulation.

Program to Access Variables

<?php 

class Member { 
	private $profilenm; 
	private $membertype; 

	public function getFirstName() { 
		return $this->profilenm; 
	} 

	public function setFirstName($profilenm) { 
		$this->profilenm = $profilenm; 
		echo("First name is set to ".$profilenm); 
		echo("<br>"); 
	} 

	public function getMemberType() { 
		return $this->membertype; 
	} 

	public function setMemberType($membertype) { 
		if ('Male' !== $membertype and 'Female' !== $membertype) { 
			echo('Set membertype as Male or Female for membertype'); 
		} 

		$this->membertype = $membertype; 
		echo("MemberType is set to ".$membertype); 
		echo("<br>"); 
	} 
} 

$member = new Member(); 
$member->setFirstName('Jalpa'); 
$member->setMemberType('Female'); 

?> 

PHP program for encapsulation

<?php 

class PAKA { 

	private $memberId; 
	private $pwd; 
	
	// Update PAKA password 
	public function updatePwd($memberId, $pwd) { 
		
		echo("Function to update password '"
				. $pwd . "' for member " . $memberId); 
		
		echo "<br>"; 
	} 

	public function courseName($memberId) { 
		 
		echo("Function to check course name of member "
				. $memberId); 
		
		echo "<br>"; 
	} 
} 

$obj = new PAKA(); 
$obj -> updatePwd('PAKA12', 'pakainfos54321'); 
$obj -> courseName('PAKA06'); 

?> 

What is encapsulation with simple example in php?

<?php

class App {
     private static $_member;

     public function Member( ) {
          if( $this->_member == null ) {
               $this->_member = new Member();
          }
          return $this->_member;
     }

}

class Member {
     private $_name;

     public function __construct() {
          $this->_name = "Parag Shukla Dr..";
     }

     public function GetName() {
          return $this->_name;
     }
}

$app = new App();

echo $app->Member()->GetName();

?>

Example 1

<?php  
class member  
{  
	public $name;  
	public $salary;  
	function __construct($n, $a)  
	{  
		$this->name=$n;  
		$this->salary=$a;  
	}  
	public function setSalary($salary)  
	{  
	   
		$this->salary=$salary;  
	   
	}  
   
	public function display()    
	{  
	   
		echo  "welcome ".$this->name."<br/>";  
		return $this->salary-$this->salary;  
	   
	}  
   
}  
   
	$member=new member("sejal",32);  
	$member->setSalary(18500);  
	echo "You are ".$member->display()." years old";  
   
?>  

I hope you get an idea about encapsulation in php.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Read Also:  Ajax Autocomplete Live Search Suggestion using PHP MySQL

Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  Laravel 6 Set Timestamps using Eloquent Model
  2. Read Also:  Laravel 6 Group By Timestamps using Query Builder
  3. Read Also:  Simple PHP Data Type Conversion Example
  4. Read Also:  jQuery Ajax GET & POST REQUEST Methods PHP MySQLi
  5. Read Also:  Print Debugging Queries in Laravel Example
  6. Read Also:  PHP Class Inheritance call parent constructor
  7. Read Also:  Validate is not a function in laravel
  8. Read Also:  Difference between two dates in years, months, days in PHP
  9. Read Also:  Laravel Custom Namespaces Example Tutorial
  10. Read Also:  PHP Class Inheritance call parent constructor

Leave a Comment

Your email address will not be published. Required fields are marked *