The allocation of PHP objects in memory ____php

Source: Internet
Author: User
The allocation of PHP objects in memory

Like in PHP and integral type, floating-point type, but also a data class, are stored in different types of data, in the runtime to be loaded into memory to use, then the object in memory how to embody it. The memory is logically roughly divided into 4 segments, stack space segment, heap space segment, code segment, initialization static segment, the different declarations in the program are placed in different memory segments, data segment (segment) is usually used to store the initialization of the program and not 0 of the global variables such as: Static Variables and constants ; Code Snippets (Segment/text segment) usually refer to a single area of memory used to hold program execution code, such as functions and methods, where a stack space segment is a place to store data types that occupy the same length of space and take up less space, such as integral type 1,10,100, 1000,10000,100000 and so on, in the memory occupies the space is equal long, are 64 bits 4 bytes. So the data length is indefinite, and the data type that occupies a large space is placed in that segment of memory. Such data is placed in the heap memory. The following figure:

Stack memory can be accessed directly, while heap memory is not directly accessible to memory. The number of our objects is a large data type and occupy the indefinite length of the type, so that the object is placed in the heap, but the object name is placed in the stack, so that the object name can use the object. $p 1 is our example of the object name, similarly, $p 2, $p 3 is also our instance of the object name, a class can be an instance of multiple objects, each object is independent, the above code equivalent to the example of 3 people come, everyone is no contact, can only show that they are human, every Individuals have their own names, gender and age attributes, everyone has a way to speak and walk, as long as the class is reflected in the member properties and member methods, instantiated objects contained in these attributes and methods.

$p 1=new person ();

For this code, $p 1 is the object name in the stack memory new person () is the real object is in the heap memory, the specific look at the following figure:

The $p1=new person () can be seen from the above figure. The equal sign right is the real object instance, the entity inside the heap memory, there are 3 new person () in the graph, so it will open up 3 spaces in the heap, produce 3 instance objects, each object is independent each other, use own space, In PHP, as long as there is a new keyword will be instantiated out of an object, in the heap to open up a piece of their own space. Each instance object in the heap is a storage attribute, for example, now the instance object inside the heap contains the name, gender, and age. Each attribute also has an address. $p 1=new person (); the right $p1 of the equal sign is a reference variable, assigning the first address of an object to the "$p 1" reference variable by the assignment operator "=", so $P1 is the variable that stores the first address of the object, $p 1 is placed inside the stack memory, $p 1 is equivalent to a pointer to an object inside the heap, So we can manipulate objects by $P1 This reference variable, and we usually call object references as objects.

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.