The conventional method is copied for each object or PHP optimizes and makes a copy of the methods for objects?

0 like 0 dislike
13 views
class Foo { function method() { echo 'foo'; } } $a = new Foo; $a->method(); $a = new Foo; $a->method();

It copies a method from class, or is one method that refer the objects?
by | 13 views

4 Answers

0 like 0 dislike
Each function in PHP has two contexts: Static and object. The method is a normal named function, which pre-announced both of these context. Thus we get that the method and the memory allocation for it, too, is one thing, but for different objects changes the context (i.e. reference to this), and then creating a new object is the only allocation of this context for the method, i.e. the stupid one address.

In addition, creating a new object the memory is not allocated at all. If the object is identical to the previous one, the address of the new object corresponds to the previous marked separation of the ZVAL structures if the mutations (to a certain time, even the bug was when the function spl_object_hash return the same string for two different objects that contain the same data). During the object change the reference to the changed field is divided, and the new value trying to take the memory of those variables (remember that the structure of a ZVAL common to all types of variables) that have been marked for destruction via GC (i.e. refcount=0), it allows you to get rid of unnecessary memory allocations. This pattern of behavior, for example, allows you to do the clone operator (clone) and instantiate (using the new operator) objects without any memory allocations at all.

P. S. So it was in some of 5.x branches, when I investigated the behavior of the blaze. Now when the nose 7.3 - I have XS, a lot has changed, although most work has been done to reduce memory consumption zval objects, DCE optimizations and AVX/SSE instructions, so don't think that much.
by
0 like 0 dislike
of course the same method)) otherwise it will be a waste of memory
by
0 like 0 dislike
You seem to think is absolutely not about that...
Can you describe the context in which I ask this question?
by
0 like 0 dislike
The objects are different, the method is the same.
When you the second time cause new to already defined variable, memory is allocated for the new object, the object is re-created. The memory address, where he referred to this $a is cleaned by the garbage collector as its capabilities.
PS: new literally says "take up system resources, and build me a facility for drawing Foo!!!"
by

Related questions

0 like 0 dislike
4 answers
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
asked Apr 14, 2019 by freeman0204
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
110,608 questions
257,186 answers
0 comments
33,569 users