Php object-oriented full strategy (2) Object Instantiation using object members

Source: Internet
Author: User

5. How to instantiate an object
As we have mentioned above, the Unit of the object-oriented program is the object, but the object is instantiated through the class, since
Our class will be declared, and the next step is to instantiate the object.
After the class is defined, we use the new keyword to generate an object.
Code snippet
Copy codeThe Code is as follows:
$ Object name = new Class Name ();
<? Php
Class Person {
// The following are the member attributes of a person.
Var $ name; // the name of a person.
Var $ sex; // gender of a person
Var $ age; // age of a person
// The following is the member method of a person.
Function say (){
// How this person can speak
Echo "This person is talking ";
} Function run (){
// How this person can walk
Echo "This person is walking ";
}
}
$ P1 = new Person ();
$ P2 = new Person ();
$ P3 = new Person ();
?>
$ P1 = new Person ();

This code is the process of generating instance objects through classes. $ p1 is the object name of our instance. Similarly, $ p2,
$ P3 is also the name of the object from our instance. A class can generate multiple objects from the instance, and each object is independent.
The code above is equivalent to three people coming out of the instance. There is no connection between each person. It only means that they are all human beings.
Each person has his/her own name, gender, and age. Each person has a way to speak and walk, as long as it is inside the class.
The existing Member attributes and member methods include these attributes and methods in the instantiated object.
Similar to the integer and floating point types in PHP, PHP is also a data type that is used to store different types of data,
When running, all objects must be loaded to the memory for use. How is the object reflected in the memory? The memory is logically
Generally, it is divided into four sections: stack space segment, heap space segment, code segment, and initialization static segment. Different declarations in the program
Put it in different memory segments. Stack space segments are data types in which storage occupies the same space length and occupies a small amount of space.
Such as integer, and so on,
All are 64-bit and 4-byte. The data length is not long, and the data type that occupies a large space is stored in that memory.
In that segment? Such data is stored in the heap memory. Stack memory can be directly accessed, while stack memory is
Memory that cannot be directly accessed. The number of our objects is a big data type and the occupied space is not long.
Object is put in the heap, but the object name is put in the stack, so that through the object name can
To use the object.
$ P1 = new Person ();
For this code, $ p1 is the object name in the stack memory, and new Person () is the real object in the heap memory.
For details, see:
= 700) window. open ('/upload/2009093021520.451.gif'); "src =" http://www.bkjia.com/uploads/allimg/131016/09212313K-0.gif "onload =" if (this. width> '200') this. width = '000000'; if (this. height> '20140901') this. height = '000000'; "border = 0>
From this we can see that $ p1 = new Person (); the right side of the equal sign is a real object instance, the entity in the heap memory,
There are three new persons () in total, so three spaces will be opened in the heap to generate three instance objects, each of which
They are independent from each other and use their own space. In PHP, if there is a new keyword
Instantiate an object and open up a space in the heap.
Each instance object in the heap is stored as a property. For example, the instance object in the heap contains a surname.
Name, gender, and age. Each attribute has an address.
$ P1 = new Person (); $ p1 on the right of the equal sign is a reference variable. The first address of the object is obtained through the value assignment operator "= ".
Assigned to the reference variable "$ p1", so $ p1 is the variable that stores the first address of the object, $ p1 is placed in the stack memory, and $ p1 is equivalent
So we can use the reference variable $ p1 to operate the object.
It is called an object reference.
6. How to Use members in an object
The above shows that the PHP Object has two types of members: Member attributes and member methods. Object we can
To declare, $ p1 = new Person (); How to Use object members? To access members of an object, you must use
Special Operator "->" to complete access to object members:
Object-> attribute $ p1-> name; $ p2-> age; $ p3-> sex;
Object-> method $ p1-> say (); $ p2-> run ();
For example:
Code snippet
Copy codeThe Code is as follows:
<? Php
Class Person {
// The following are the member attributes of a person.
Var $ name; // the name of a person.
Var $ sex; // gender of a person
Var $ age; // age of a person
// The following is the member method of a person.
Function say () {// method in which this person can speak
Echo "This person is talking ";
}
Function run () {// how this person can walk
Echo "This person is walking ";
}
}
$ P1 = new Person (); // create an instance object $ p1
$ P2 = new Person (); // create an instance object $ p2
$ P3 = new Person (); // create an instance object $ p3
// The following three rows assign values to $ p1 Object Attributes
$ P1-> name = "James ";
$ P1-> sex = "male ";
$ P1-> age = 20;
// The following three rows are the attributes of the $ p1 object.
Echo "the name of the p1 object is:". $ p1-> name. "<br> ";
Echo "the gender of the p1 object is:". $ p1-> sex. "<br> ";
Echo "the age of the p1 object is:". $ p1-> age. "<br> ";
// The following two rows access the method in the $ p1 object
$ P1-> say ();
$ P1-> run ();
// The following three rows assign values to $ p2 object attributes.
$ P2-> name = "Li Si ";
$ P2-> sex = "female ";
$ P2-> age = 30;
// The following three rows are the attributes of the $ p2 object.
Echo "p2 Object name:". $ p2-> name. "<br>"
Echo "p2 object Gender:". $ p2-> sex. "<br> ";
Echo "p2 object age:". $ p2-> age. "<br> ";
// The following two rows access the method in the $ p2 object
$ P2-> say ();
$ P2-> run ();
// The following three rows assign values to $ p3 Object Attributes
$ P3-> name = "Wang Wu ";
$ P3-> sex = "male ";
$ P3-> age = 40;
// The following three rows are the attributes of the $ p3 object.
Echo "p3 Object name:". $ p3-> name. "<br> ";
Echo "the sex of the p3 object is:". $ p3-> sex. "<br> ";
Echo "age of p3 objects:". $ p3-> age. "<br> ";
// The following two rows access the method in the $ p3 object
$ P3-> say ();
$ P3-> run ();
?>

From the above example, we can see that only the Members in the object need to access the object in the form of Object-> attribute, object-> method, and then
There is no second method to access the members in the object.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.