Website Templates Search

Php encapsulation basics

  • Click to view live demo By Admin
  • Click to view live demo Jan 05, 2019

Wrapping up “Data Member” and “Member Function” in a single unit is known as encapsulation.

Example: ( Class ).

you can define encapsulation in two phases :

1. you can wrapper your data in a container.

Example : you can bind  up your “Properties” and “methods” in a class.

i.e you can bind your functions and properties in encapsulated form within the class ( Container ).

With the help of encapsulation you can hide your information, this concept is Known as “Data Hiding” ( Provide data secureness ).

2. In encapsulation it is best practice to using getters()  to get the value of properties and setters() to set the value of properties.


<?php

	class BankAccount{
	
		private $Accno;
		private $AccName;
		private $balance;
	
		public function __construct($acno,$accname){
		   $this->Accno = $acno;
		   $this->AccName = $accname;
		   $this->balance =0;
		}
		
		public function Deposit($amt){
		   if($amt<5000){
			 return "Amount is less than 5000";
		   }else{
			 return $this->balance = $amt;
			}
		}
		
		public function getBalane(){
		  return $this->balance;
		}
		
		public function Withdraw(){
		  echo "Withdraw Method:";
		}
	}
	
	$obj = new BankAccount(12345,'Bank1');	
	$obj->Deposit(12000);
	echo $obj->getBalane();
	
?>

------------ ****** ------------

OUTPUT:
        12000