Process oriented
Classes and objects
Object: Anything can be called an object, something instantiated by a class
Class: Something abstracted from all the same objects
To define a class:
classren{ Public$Name;//member Variables protected$age; Private$height; var$sex;//generally do not assign a value to a member variablefunction Run ()//member functions (methods){echo"this man is running! "; } function Say () {echo $ This->name."I 'm talking! "; }}
Using classes
// 1. Instantiating an object $r New Ren ("male"); // 2. Assigning values to member variables (calling member variables) $r // assign a value to the name of the object $r->age; // value//3. Calling member Methods $r->say (); // Execute member Method
Access modifiers
1. If you want to add access modifiers, you need to remove Var
2. Three kinds: public protected protected private private
3. If you do not add an access modifier, the default is public
1 Public $Name // public 2protected$age// protected 3 Private$height// proprietary 4var$sex //
$this references
$this represents the object (which object is called) and does not represent the class
constructor function
1. Special wording: __construct ()
2. Special execution: The first thing to do when creating objects
Action: Initialize an Object
1 classRen2 {3 Public $Name;4 protected $age; 5 Private $height;6 var $sex; 7 8 function__construct ($s)//constructor Function9 {Ten $this->sex =$s; One } A } - $r=NewRen ("Male"); - Var_dump($r);
destructor, which executes before the object is destroyed
1 classRen2 {3 Private $name;4 Private $sex;5 Private $age;6 Public functionSay ()7 {8 Echo"Hello";9 }Ten function__destruct ()//destructor, which executes before the object is destroyed One { A Echo"The object was destroyed!" "; - } - } the - $r=NewRen ("Male"); - $r->say ();
Object-oriented three major features
Packaging
Purpose: To make the class more secure and not allow the outside world to directly access the member variables inside the class
Practice:
1. Make the member variables private
2. Do a method to implement the value of the variable or assignment, in the method to add a restriction condition
Inherited
Polymorphic
Assignment value:
1. Common Methods
1 classRen2 {3 Private $name;4 Private $sex;5 Private $age;6 functionSetage ($a)//a function that assigns a value to age7 {8 if($a>10 &&$a<50)9 {Ten $this->age =$a; One } A } - } - the $r=NewRen (); - $r->setage (40);
2. Magic method
classren{Private $name; Private $sex; Private $age; function__set ($name,$value)//Magic method of assigning a value to a private member inside a class { if($name= = "Age") { if($value>20 &&$value<50) { $this-$name=$value; } } Else { $this-$name=$value;//Assign Value } }$r=NewRen ();$r->age = 40;
Value:
1. Common methods
classren{Private $name; Private $sex; Private $age; functionGetage ()//take the value of age { return $this->age;//returns the value of an age } $r=NewRen ();Echo $r->age;
2. Magic method
1 classRen2 {3 Private $name;4 Private $sex;5 Private $age;6 function__get ($name)//Magic method for taking a value on a private member inside a class7 {8 return $this-$name;9 }Ten } One $r=NewRen (); A Echo $r->sex;
Using the Magic method provided in the class can also implement the operation of the private member
__set () Feature: will automatically execute, the variable name in the assignment statement as the first parameter, the value of the variable as a second parameter call __set method
__get () Feature: will automatically execute, the variable name in the value statement as a parameter call __get method
PHP Object-oriented