First Glimpse of PHP object-oriented
1. Class: A class is a collection of properties and methods is an abstract concept such as "people" in life, "cars"
2. Object: Objects are specific things like a guy called "Jack Bauer." A car called "Audi A7".
3. Class and object relationships: classes are abstract objects that come out of a collection object that is a concrete class
Example 1: Creating a Person class
The first step: declaring a person class
classperson{//the member property of the person class declares the property of the class to be used with the VAR keyword var $name; var $age; var $sex; //member methods for the person class functionsay () {Echo"This man speaks again."; } functionrun () {Echo"This man is walking."; } }
Step Two: Instantiate an object
// The New keyword creates an instance $person 1=new person (); // An instance of a person class was created $person 2=new person ();
Step three: Assigning values to Object members
// object property Assignment (using)//$person1 object property initialization assignment; $person 1->name= "Zhang San"; $person 1->age=25; $person 1->sex= "male"; // The following gives the object $person2 object property initialization assignment; $person 2->name= "John Doe"; $person 2->age=23; $person 2->sex= "female";
Fourth step: Accessing objects
//here are the properties in the Access Object $person1 objectEcho"The Person1 object name is:".$person 1->name, "<br>";//outputs the value of $name in the $person1 object;Echo"Person1 object Age is:".$person 1->age, "<br>";//outputs the value of $age in the $person1 object;Echo"Person1 Object Gender is:".$person 1->sex, "<br>";//outputs the value of $sex in the $person1 object;//The following is a way to access an object $person1 objectEcho"<br>";$person 1->say ();Echo"<br>";$person 1-run ();Echo"<br>";//here are the properties in the Access Object $person2 objectEcho"<br>";Echo"The Person2 object name is:".$person 2->name, "<br>";//outputs the value of $name in the $person2 object;Echo"Person2 object Age is:".$person 2->age, "<br>";//outputs the value of $age in the $person2 object;Echo"Person2 Object Gender is:".$person 2->sex, "<br>";//outputs the value of $sex in the $person2 object;
Example 2: Create a phone class (full)
//declaring a class for a telephone phone classphone{var $Manuefatuures; var $color; var $Battry _capacity; var $Screen _size; functionCall () {Echo"Making a call."; } functionmessage () {Echo"Sending SMS"; } functionPlay_music () {Echo"Playing music"; } functionphoto () {Echo"Taking pictures."; } } Echo"<br><br> the object of a phone <br>"; //create an instance of the phone object$phone 1=NewPhone ();$phone 1->manuefatuures= "Huawei P6";$phone 1->color= "BLACK";$phone 1->battry_capacity=3000;$phone 1->screen_size= "360*480";Echo $phone 1->manuefatuures. " <br> ";Echo $phone 1->color. " <br> ";Echo $phone 1->battry_capacity. " <br> ";Echo $phone 1->screen_size. " <br> ";