1

I will try to be succinct on this:

I have this:

Example1:

$obj1 = new SimpleObj();
$obj2 = $obj1;
$obj2 = null;

Example2:

$obj1 = new SimpleObj();
$obj2 = clone $obj1;
$obj2 = null;

Example3:

$obj1 = new SimpleObj();
$obj2 =& $obj1;
$obj2 = null;

I did a lot of research on this, but i'm not assured yet. Which one will actually destroy the object and free memory?

Thank in advance!

Valentoni
  • 308
  • 4
  • 19
  • 1
    Destroy which object? Examples 1 and 2 destroy $obj2, and example 3 destroys $obj1 by destroying $obj2 which references to it. – Sergey Vidusov Feb 21 '16 at 08:19
  • PHP is a garbage collected language. You have no control over when memory is freed. If you just want to know when an object is destroyed you can add the `__destruct` function to your class. It will be called when then object is destroyed. – Sverri M. Olsen Feb 21 '16 at 08:22
  • 1
    Maybe this link will help you [best way to destroy php object](http://stackoverflow.com/questions/8798443/best-way-to-destroy-php-object#answers-header) – Afkari Feb 21 '16 at 08:31
  • In example 1 - you destroy the variable $obj2 that contain the address to the object. same goes for example 2. in example 3 you destroy $obj1 because $obj2 value is the address of $obj1 due to the & – zion ben yacov Feb 21 '16 at 08:53

0 Answers0