1

Simple question; I'm doing the following in an abstract PHP class of mine, but I have no idea whether it's actually being called/doing anything:

abstract class Curl {

    protected $curl;

    public function __construct()
    {
        $this->curl = curl_init();
    }

    public function __destruct()
    {
        curl_close($this->curl);
    }
}

I've read various online posts about whether __destruct actually gets called, so I'm wondering if this is something I should be doing?

Alias
  • 2,983
  • 7
  • 39
  • 62
  • php cleans up after itself when a script exits. the only time you'd need to explicitly close the curl handle is if you're doing a LOT of curl usage in a loop and initializing a fresh curl is the easiest way to get back to "normal" options/configurations. – Marc B Aug 25 '14 at 21:12
  • According to this QA, it's unclear if a destructor is absolutely guaranteed to be called in every circumstance: http://stackoverflow.com/questions/151660/can-i-trust-php-destruct-method-to-be-called - personally I'd call `curl_close` after each curl operation, not in a class destructor. – Dai Aug 25 '14 at 21:13

1 Answers1

0

Since it is not initialised, it does not call __destruct:

<?php

class test
{
    static $t = null;
}

abstract class Curl {

    protected $curl;

    public function __construct()
    {
        $this->curl = curl_init();
    }

    public function __destruct()
    {
        var_dump( curl_close( $this->curl ) );
        test::$t = curl_close( $this->curl );
    }
}

class ConcreteClass extends Curl
{

}
var_dump( test::$t );

?>
Kohjah Breese
  • 4,008
  • 6
  • 32
  • 48
  • 1
    For clarification to OP, you would need concrete classes to call `parent::__destruct()` in their own destructors, as children do not inherit constructor/desctructor methods from parents. – Mike Brant Aug 25 '14 at 21:28