The allocation of PHP objects in memory

Source: Internet
Author: User

Read a blog post on the internet about the allocation of PHP objects in memory, specially recorded, and then slightly modified.

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 segment, and the different declarations in the program are placed 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;
Code
segment/text segment usually refers to a 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.
Heap Memory data is variable in length and occupies a large data type. Such as:

stack memory can be accessed directly, and heap memory is not directly accessible. 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.

/*
* The following three variables are independent and are not related to each other
* $p 1, $p 2, $p 3 is placed in the stack memory, the new person is placed in memory, through $P1, $p 2, $p 3 can access their new person
**/
$p 1=new person (); $p 2=new person (); $p 3=new person ();

For our object is a kind of large data type and take up a variable 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 be used by the object.
$p 1 is the name of the object we have instantiated, the same, $p 2, $p 3 is also the name of the object we come up with, a class can instantiate multiple objects, each object is independent,
The above code is equivalent to an instance out of 3 people, there is no connection between each person, can only show that they are human, everyone has their own name, gender and age attributes, everyone has a way to speak and walk, as long as the class embodies the member properties and member methods, These attributes and methods are included in the instantiated object.

Please see:

As you can see, the right side of the equals sign is the real instance object, which is the object entity stored in the heap memory.

A total of three new person, so will be in the heap memory to open up three separate space, the production of 3 instance objects, each instance object is independent, exclusive 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 left $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 $P1 This reference variable is used to manipulate the object, and we usually refer to it as an object.

Finally, do an easy-to-confuse verification:

classperson{ Public $name;}$obj 1=NewPerson ();$obj 1->name = "Test1";Echo $obj 1-name;$obj 2=$obj 1;$obj 2->name = "Test2";Echo $obj 1-name;Echo $obj 2->name;

$p 1is a pointer to an object and not the object itself , obj2 and obj1 all point to the same piece of memory, The same object . This is the same as the OOP language

Object(Person)[2]
Public' Name '=String' Test2 '(length=5)

Object(Person)[2]public=>< Span style= "FONT-SIZE:10.5000PT; Font-family: ' Times New Roman '; > string  Test2 '  

If you want a copy of an object, use $obj 2 =clone $obj 1; Using clone will create a new object, allocating memory, independent of the original obj1
$obj 2 = $obj 1;
$obj 2 = & $obj 1;
The same effect, the same explanation?


For object, it is the same. It is not the same for normal variables.
$a = 1;
$b = $a;
$c = & $a;
Not the same.

The allocation of PHP objects 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.