<body><?PHP//concept of class//object concept//Definition class//class ren//{//Member variable//member method//}//object//$r = new Ren ();//The member of the calling object $r->//three main features: encapsulation, inheritance, polymorphic//1. Package// Purpose: To make the class more secure. Practice: Do not allow direct access to the members of the class//specific practices://1. The member becomes private: the access modifier (Public private protected)//2. Causes the member method to manipulate the variable//3. Use the __get () and __set () methods provided in the classclassren{Private $age;//Private Members Private $name; Private $sex; //constructor: Initializes the member variable when the object is created//1. Execution time is special: The//2 is executed automatically when the object is created. Special: __construct function__construct ($s) { $this->sex =$s; } //to assign values to variable $age /*Public function Setage ($a) {if ($a >18 and $a <50) {$this->age = $a; }}//Take value function Getage () {return $this->age; }*/ //the method of assigning a value to a variable function__set ($n,$v) { if($n= = "Age") { if($v>18 and$v<50) { $this-$n=$v; } } Else { $this-$n=$v; } } //method of taking value function__get ($n) { return $this-$n; } }$r=NewRen ("Male");$r->name = "Zhang San";Var_dump($r);//design a class: Contains $ A, $b, sum method, the method of product, you can initialize the variable, $a, $b must be greater than 0 less than?></body>View Code
PHP Object-oriented encapsulation