Encapsulation in PHP


Prev Tutorial Next Tutorial

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

Prev Tutorial Next Tutorial

Google Advertisements
Download Projects

Google Adsense Advertisements

Buy 3 Year Old Domain, Tutorialday.com, SeosmoTools.com @$200

Buy This Ad Space @$50 per Month, Ad Size 300X250 Contact on: hitesh.xc@gmail.com WhatsApp no. 8882538826

Yahoo Advertisements