Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php destructor behaviour

im trying to understand php constructor and destructor behaviour. Everything goes as expected with the constructor but i am having trouble getting the destructor to fire implicitly. Ive done all the reading on php.net and related sites, but i cant find an answer to this question.

If i have a simple class, something like:

class test{

     public function __construct(){
          print "contructing<br>";
     }

     public function __destruct(){
          print "destroying<br>";
     }
}

and i call it with something like:

$t = new test;

it prints the constructor message. However, i'd expect that when the scripts ends and the page is rendered that the destructor should fire. Of course it doesnt.

If i call unset($t); when the scripts ends, of course the destructor fires, but is there a way to get it to fire implicitly?

like image 417
kris Avatar asked May 06 '10 01:05

kris


People also ask

What does destructor do in PHP?

A destructor is called when the object is destructed or the script is stopped or exited. If you create a __destruct() function, PHP will automatically call this function at the end of the script. Notice that the destruct function starts with two underscores (__)!

What are the __ construct () and __ destruct () methods in a PHP class?

It is defined inside the class and is used to automatically call when the object is created. PHP4 provides the constructor method whereas PHP5 provides the magic method __construct and __destruct. This method is automatically called when an object is created or destroyed.

Why we use $this in PHP?

$this is a reserved keyword in PHP that refers to the calling object. It is usually the object to which the method belongs, but possibly another object if the method is called statically from the context of a secondary object. This keyword is only applicable to internal methods.


1 Answers

This is pretty easy to test.

<?php

class DestructTestDummy {
    protected $name;

    function __construct($name) {
        echo "Constructing $name\n";
        $this->name = $name;
    }

    function __destruct() {
        echo "Destructing $this->name\n";
        //exit;
    }
}

echo "Start script\n";

register_shutdown_function(function() {
    echo "Shutdown function\n";
    //exit
});

$a = new DestructTestDummy("Mr. Unset");
$b = new DestructTestDummy("Terminator 1");
$c = new DestructTestDummy("Terminator 2");

echo "Before unset\n";
unset($a);
echo "After unset\n";


echo "Before func\n";
call_user_func(function() {
    $c = new DestructTestDummy("Mrs. Scopee");
});
echo "After func\n";

$b->__destruct();

exit("Exiting\n");

In PHP 5.5.12 this prints:

Start script
Constructing Mr. Unset
Constructing Terminator 1
Constructing Terminator 2
Before unset
Destructing Mr. Unset
After unset
Before func
Constructing Mrs. Scopee
Destructing Mrs. Scopee
After func
Destructing Terminator 1
Exiting
Shutdown function
Destructing Terminator 2
Destructing Terminator 1

So we can see that the destructor is called when we explicitly unset the object, when it goes out of scope, and when the script ends.

like image 109
mpen Avatar answered Sep 22 '22 04:09

mpen