Object-Oriented encapsulation: 1, is to combine the object's members (attributes, methods) into a separate same unit, and as far as possible to hide the internal details of the object public protected private, with this keyword decorated members, can only be accessed within the object (only with $this access ), cannot be used outside of the object example:
Class person{
private $name;
Private $age;
Private $sex;
function __construct ($name = "", $age =20, $sex = "male") {
$this->name= $name;
$this->age= $age;
$this->sex= $sex;
}
function Getpro ($name) {return
$this-> $name;
}
function Setage ($age) {
if ($age >100$age<0) {return
;
}
$this->age= $age;
}
function Getage () {
if ($this->age<30) {return
$this->age;
} ElseIf ($this->age<40) {return
$this->age-5;
} ElseIf ($this->age<50) {return
$this->age-10;
} else{return
$this->age-15;
}
}
function say () {
echo "My name is:". $this->name. ", The Age is:". $this->age. ", Sex is:". $this->sex. ' <br> ';
}
function __destruct () {
echo $this->name. ", Goodbye". " <br> ";
}
}
$p 1=new person ("rayhooo", "male");
$p 1->say ();
echo $p 1->getpro ("name"). ' <br> ';
$p 1->setage ();
echo $p 1->getage (). ' <br> ';