Php object-oriented (1) first glance (basic introduction to php object-oriented)
Php object-oriented
1. Class: class is the set of attributes and methods. It is an abstract concept, such as "people" and "car" in life"
2. Objects: objects are specific things, such as a person named "Xiaoqiang" and a car named "Audi a7 ".
3. Relationship between classes and objects: A class is a collection object of an abstract thing.
Instance 1: Create a Person class
Step 1: declare a Person class
Class Person {// The member attribute of the Person class declares that the class attributes should use the var keyword var $ name; var $ age; var $ sex; // function say () of the member method of the Person class () {echo "This person speaks again";} function run () {echo "This person is walking ";}}
Step 2: instantiate an object
// New Keyword creates an instance $ person1 = new Person (); // creates an instance of the person class $ person2 = new Person ();
Step 3: assign values to object members
// Assign a value to an object attribute (use->) // assign a value to the object $ person1 object attribute initialization; $ person1-> name = "Zhang San"; $ person1-> age = 25; $ person1-> sex = "male"; // assign a value to the object $ person2 object attribute initialization; $ person2-> name = "Li Si"; $ person2-> age = 23; $ person2-> sex = "female ";
Step 4: access objects
// The following is the access object $ person1 object's attribute echo "person1 Object Name :". $ person1-> name, "<br>"; // output $ name in $ person1; echo "Age of person1 :". $ person1-> age, "<br>"; // output the $ age value in the $ person1 object; echo "the gender of the person1 object is :". $ person1-> sex, "<br>"; // output the value of $ sex in the $ person1 object; // The following is the method echo for accessing the $ person1 object "<br>"; $ person1-> say (); echo "<br> "; $ person1-> run (); echo "<br>"; // The following is the attribute echo in the $ person2 object: "<br>"; echo "The Name Of The person2 object is: ". $ person2-> name, "<br>"; // output $ name in $ person2; echo "Age of person2 :". $ person2-> age, "<br>"; // output the $ age value in the $ person2 object; echo "the gender of the person2 object is :". $ person2-> sex, "<br>"; // output the value of $ sex in the $ person2 object;
Instance 2: Create a phone class (complete)
// Declare the class Phone of a Phone number {var $ Manuefatuures; var $ color; var $ Battry_capacity; var $ Screen_size; function call () {echo "calling ";} function message () {echo "Sending text message";} function Play_music () {echo "playing music";} function photo () {echo "taking photo ";}} echo "<br> object of a mobile Phone <br>"; // create an instance of the Phone object $ phone1 = new Phone (); $ phone1-> Manuefatuures = "huawei p6"; $ phone1-> color = "black"; $ phone1-> Battry_capacity = 3000; $ phone1-> Screen_size = "360*480"; echo $ phone1-> Manuefatuures. "<br>"; echo $ phone1-> color. "<br>"; echo $ phone1-> Battry_capacity. "<br>"; echo $ phone1-> Screen_size. "<br> ";
Php object-oriented (2) $ this
<! Doctype html>
The above is a brief introduction to php object-oriented, which is suitable for new friends.