PHP object-oriented programming full tutorial: 5. how to instantiate an object?

Source: Internet
Author: User
: This article mainly introduces PHP object-oriented programming full tutorial: 5. how to instantiate an object ?, If you are interested in the PHP Tutorial, refer. 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.

After the class is defined, we use the new keyword to generate an object.

$ Object name = new class name ();


 


$ P1 = new Person ();

This code is the process of generating instance objects through classes. $ p1 is the object name of our instance. Similarly, $ p2 and $ p3 are also the object names of our instances, A class can instance multiple objects, each of which 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 the member attributes and member methods embodied in the class, the instantiated object contains these attributes and methods.

Like in PHP, it is also a data type that stores different types of data, and must be loaded to the memory during running, how is the object reflected in the memory? Memory is generally divided into four segments, stack space segments, heap space segments, code segments, and initial static segments. different declarations in the program are placed in different memory segments, stack space segments are data types that occupy the same length of storage space and small space, such as integer 1, 10,100,100 0, 10000,100 000, and so on, all are 64-bit and 4-byte. So the data length is not long, and the data type that occupies a large space is put in that memory segment? Such data is stored in the heap memory. Stack memory can be directly accessed, while Stack memory cannot be directly accessed. For our objects, it is a type of big data type that occupies an indefinite amount of space. Therefore, objects are placed in the heap, but object names are placed in the stack, in this way, the object can be used through the object name.

$ 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:


It can be seen that $ p1 = new Person (); the right side of the equal sign is a real object instance. There are three new persons () in the heap memory (), therefore, three instances are created in the heap to create three instance objects. each object is independent of each other and uses its own space. in PHP, as long as there is a new keyword, an object will be instantiated and a space will be opened up in the heap.

Each instance object in the heap is stored. for example, the instance object in the heap contains the name, gender, and age. Each attribute has an address.

$ P1 = new Person (); $ p1 on the left of the equal sign is a reference variable. The first address of the object is assigned to the reference variable "$ p1" through the value assignment operator "=, therefore, $ p1 is the variable that stores the first address of an object. $ p1 is placed in the stack memory. $ p1 is equivalent to a pointer pointing to an object in the heap, therefore, we can operate on objects through the reference variable $ p1, which is also called Object reference.


The above describes the PHP Object-Oriented Programming Tutorial: 5. how to instantiate an object ?, Including some content, hope to be helpful to friends who are interested in PHP tutorials.

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.