PHP (13) Object-oriented encapsulation

Source: Internet
Author: User
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> ';  


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.