2

If I have a large object and assign another variable to that object, does php create two objects, or does it use a pointer internally?

for example:

<?php
$myObject = new Class_That_Will_Consume_Lots_Of_Memory();
$testObject = $myObject;

In this example will i be using 2 x the memory footprint of a Class_That_Will_Consume_Lots_Of_Memory instance or will it be 1 of those and a pointer?

1

2 Answers 2

1

The latter: one object and a pointer/reference (and in fact, here, two pointers/references, since the first is one as well).

To get a new object, use clone.

Related: Are PHP5 objects passed by reference?

Sign up to request clarification or add additional context in comments.

3 Comments

@Cobra_Fast Yes, for things that are normally passed by value. Instance of objects, however, are passed by reference pretty much by definition.
@Cobra_Fast: the & is not needed in php5. I believe it raises a warning or a strict notice, too.
@Cobra_Fast this was PHP4. …
0

Objects in PHP5 are passed by reference, arrays and other types passing is based on Copy on Write technique:

$a = ['a'=>'b'];
$b = $a; // Here we used 1x memory
$b['x'] = 'y'; // Now it become 2x memory

You can use memory_get_usage() to debug memory usage.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.