Php Destructors

2019-01-30 17:25发布

Please give me some real life examples when you had to use __destruct in your classes.

11条回答
女痞
2楼-- · 2019-01-30 17:57

A destructor is extremely useful if you use a custom database connector/wrapper.

In the constructor, you can pass the connection information. Because you can use a destructor (rather than a finalizer, etc.,) you can rely on that to close the connection for you. It's more of a convenience, but it certainly is useful.

For example, when PHP decides to explicitly "free" the object (i.e., it is no longer used,) it will call the destructor at that time. This is more useful in the scenario I describe as you're not waiting for the garbage collector to run and call the finalizer.

$0.02

Ian

查看更多
干净又极端
3楼-- · 2019-01-30 18:04

I use APC caching for large numbers of "low level" objects, that otherwise would use excessive memory; and I have a cacheCollection object that handles the reading and writing of those "low level" objects to and from APC during execution of the script. When the script terminates, the objects must be cleared down from APC, so I use the cacheCollection __destruct method to perform that function.

查看更多
一纸荒年 Trace。
4楼-- · 2019-01-30 18:06

I create a php page what will generate a movie information jpg file. This page will have to gather a few information and run inkscape to convert template (an svg file) to a png before converting to jpg. The svg contain relative links to other image which must be a file. So my page download necessary files into a temporary folder, convert the svg file. At the end, the temporary folder must be deleted.

I put the temporary folder deletion into the destructor. Before there can be many reason the page ends unexpected and the only think I can be sure is that destructor will be call when page exit.

Hope this helps.

查看更多
Fickle 薄情
5楼-- · 2019-01-30 18:07

For example:

<?php
class Session
{
    protected $data = array();

    public function __construct()
    {
        // load session data from database or file
    }

    // get and set functions

    public function __destruct()
    {
        // store session data in database or file
    }
};

This is a good why to use destruct. You prevents reading and writing to a session source all the time and do this only at the start and at the end.

查看更多
疯言疯语
6楼-- · 2019-01-30 18:07
<?php
class Database
{
    private $connection;
    private $cache = array();

    function __construct([$params])
    {
        //Connection here
    }

    //Query
    public function query(Query $Query)
    {
        if($this->is_cached($Query->checksum))
        {
            return $this->get_cache($Query->checksum);
        }
        //...
    }
    public function __destruct()
    {
        unset($this->connection);
        $this->WriteCache();
        unset($this->cache);
        shutdown_log($this,'Destruction Completed');
    }
}
?>

theres an example that should make you understand.

查看更多
甜甜的少女心
7楼-- · 2019-01-30 18:07

If you use handles returned by fopen() for say, logging, you can use __destruct() to make sure fclose() is called on our resources when your class is destroyed.

查看更多
登录 后发表回答