Encapsulation in PHP

Encapsulation is a concept of wrapping up or binding up related data members and methods in a single module known as encapsulation.it is essential to maintain your privacy so for security we are making private method sometime.Private method means it can be accessed withing same class. Out side class can’t access private method of other class.That’s why encapsulation is known as data hiding is the main advantage for encapsulation.


<?php

class User
{
		private  $name;
		private  $age;
 
			function __construct($pname, $page)
			 {
			$this->name=$pname;
			$this->age=$page;
			 
			}
 
		public function setAge($ag)
		{
		$this->ag=$ag;
		}
 
		public function display()
		{
		echo  "welcome ".$this->name."";
		 
		return $this->ag;
		}
}
 
$User=new User("SOurabh",27);
 
$User->setAge(18);
 
echo "You are ".$User->display()." years old";
 ?>

Leave a Reply