class and object concepts anything can be called an object, referring to specific information
A class is a commonality that abstracts out all the same objects.
Defining and Using Classes
/*The access modifier public, which is publicly available, can be called anywhere, the default entry protected protected, and can only be accessed within the class or class's subclass, only within the class.*///Defining ClassesclassRen//class name, first letter general capitalization{ Public $Name;//member Variables protected $age; Private $height; var $sex;//No access modifier preceded by Var, default is public function__construct ($a)//constructor, function name fixed cannot be changed { $this->sex=$a; } function__destruct ()//destructors, automatic execution of object destruction, fixed function name cannot be changed { Echo"The object has been destroyed."; } functionRun ()//member Methods { Echo"This man is running <br>"; } functionSay () {Echo $this->name. " is talking ";//$this Reference the data inside the class, $this which object calls which object is represented }}//using Classes$a=Newren ("male");//Initializing Objects$a->name = "Zhang San";//Assigning a value to an object member variableVar_dump($a);//All information about the output object$a->run ();//Call member Method//this reference$a-Say ();Echo"<br>";//constructor FunctionEcho $a->sex;
encapsulates one of the three main features of object-oriented (encapsulation, inheritance, polymorphism)
//encapsulation for the purpose of making the class more secure, without direct access to the outside world. The member variable is changed to private, and the indirect operation of the member variable is implemented by various methods. classRen {Private $name; Private $age; Private $height; Private $sex; functionSetage ($a)//functions for assigning class private variables { if($a>=10&&$a<=50) $this->age=$a; } functionGetage ()//functions that return class-private variables { return $this-Age ; } function__set ($name,$value)//The magic method of assigning a value to a private variable inside a class, the function name cannot be changed { if($name= = "Age") { if($value>20&&$value<100) $this-$name=$value; } Else $this-$name=$value; } function__get ($name)//Gets the Magic method returned by the private variable inside the class, the function name cannot be changed { return $this-$name; }}$a=NewRen;$a->setage (50);Echo $a-getage ();$a->name = "Zhang San"; $a->age = "40";//executing the statement automatically calls the __set () method$a->sex = ' Male ';Var_dump($a);Echo"<br>";Echo $a->name;//executing the statement automatically calls the __get () method
52nd Day Class object oriented