Encapsulation in PHP
Advertisements
Encapsulation in PHP
It is a concept of wrapping up or binding up related data members and methods in a single module known as Encapsulation. Encapsulation refers to a concept where we encapsulate all the data and member functions together to form an object.
Syntax
<?php
class person
{
public $name;
public $age;
function __construct($n, $a)
{
$this->name=$n;
$this->age=$a;
}
public function setAge($ag)
{
$this->ag=$ag;
}
public function display()
{
echo "welcome ".$this->name."
";
return $this->age-$this->ag;
}
}
$person=new person("Gaurav",27);
$person->setAge(20);
echo "You are ".$person->display()." years old";
?>
Output
welcome Gaurav
You are 28 years old
Google Advertisment
