Php object-oriented full strategy (2) object instantiation using object members

Source: Internet
Author: User
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, the next step is to instantiate the object. 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

The code is as follows:


$ Object name = new class name ();
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://files.jb51.net/upload/20090930215213451.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

The code is as follows:


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 ."
";
Echo "the gender of the p1 object is:". $ p1-> sex ."
";
Echo "the age of the p1 object is:". $ p1-> age ."
";
// 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 ."
"
Echo "p2 object gender:". $ p2-> sex ."
";
Echo "p2 object age:". $ p2-> age ."
";
// 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 "the name of the p3 object is:". $ p3-> name ."
";
Echo "the sex of the p3 object is:". $ p3-> sex ."
";
Echo "the age of the p3 object is:". $ p3-> age ."
";
// 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.