PHP Object-oriented (PHP object allocation in memory)

Source: Internet
Author: User

Like in PHP and integer, floating point type, is also a kind of data class, are stored in different types of data, in the run time to load into memory to use, then the object in memory is how to reflect it? The memory is logically divided into 4 segments, stack space segment, heap space segment, code snippet, initialization static section, the program inside different statements in different memory segments, data Segment) typically refers to a global variable that is initialized and not 0 in a program, such as static variables and constants, and code segment/text segment usually refers to a piece of memory area, such as functions and methods, that is used to store program execution code; The stack space segment is where the data types that occupy the same space length and occupy little space, such as Integer 1,10,100,1000,10000,100000 and so on, occupy space in memory as long as 64 bits and 4 bytes. So the data length is variable, and the data type with large space is placed in that segment of memory? This data is placed in the heap memory . Such as:

Stack memory is directly accessible, and heap memory is memory that cannot be accessed directly. The number of our objects is a large data type and takes up a variable length of space, so the object is placed in the heap, but the object name is placed inside the stack, so that the object name can be used. $p 1 is the object name of our example, the same, $p 2, $p 3 is also our instance of the object name, a class can be instances of multiple objects, each object is independent, the above code equivalent to the instance out of 3 people, everyone is not connected, can only show that they are human, every Individuals have their own names, gender and age attributes, and everyone has a way to speak and walk, as long as the class is reflected in the member attributes and member methods, the object is included in the instantiation of these properties and methods.

$p 1=New Person ();

For this code, $p 1 is the object name in the stack memory inside the new person () is the real object is inside the heap memory, the concrete see:

You can see $p1=new person (); The right side of the equals sign is a real object instance, in the heap memory of the entity, a total of 3 times New person (), so will be in the heap open 3 space, produce 3 instance objects, each object is independent of each other, using their own space, in PHP Inside, as long as there is a new keyword appears to instantiate an object, in the heap to open up a piece of their own space. Each instance object in the heap stores properties, for example, the instance object inside the heap now contains names, genders, and ages. Each property also has an address. $p 1=new person (); the right $p1 of the equals sign is a reference variable that assigns the first address of the object to the reference variable "$p 1" through the assignment operator "=", so $P1 is the variable that stores the first address of the object, $p 1 is placed in the stack memory, $p 1 is equivalent to a pointer pointing to the object inside the heap, So we can manipulate the object by $P1 this reference variable, and we usually refer to it as an object.

PHP Object-oriented (PHP object allocation in memory)

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.