what is encapsulation with an example

Encapsulation example is give below for the object oriented programming and it is best used for database security and other hacking prevention purposes.Acutually encapsulation is a big topic and needs hundred of hours study for the full grasp of encapsulation and what does it do in oop and and what level we use it.

however there is an example given below for the nice and easy way to understand it 


//we are creating a class

class Me {
private static $_users;

public function User( ) {
if( $this->_users == null ) {
$this->_users = new User();
return $this->_user;


class User {
private $_name;

public function __construct() {
$this->_name = “i am dr.”;

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

$Mine = new Me();

//$Mine is being used for accessing the GetMyName ()  ; this is called Encapsulation//

echo $Mine->User()->GetMyName();