Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:73853 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 11488 invoked from network); 2 May 2014 15:08:31 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 2 May 2014 15:08:31 -0000 Authentication-Results: pb1.pair.com header.from=johannes@schlueters.de; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=johannes@schlueters.de; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain schlueters.de from 217.114.215.10 cause and error) X-PHP-List-Original-Sender: johannes@schlueters.de X-Host-Fingerprint: 217.114.215.10 mail.experimentalworks.net Received: from [217.114.215.10] ([217.114.215.10:53718] helo=mail.experimentalworks.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id F9/DD-16678-DE4B3635 for ; Fri, 02 May 2014 11:08:30 -0400 Received: from [192.168.2.31] (ppp-93-104-24-209.dynamic.mnet-online.de [93.104.24.209]) (using TLSv1 with cipher ECDHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) (Authenticated sender: johannes@schlueters.de) by mail.experimentalworks.net (Postfix) with ESMTPSA id 641D146820; Fri, 2 May 2014 17:09:22 +0200 (CEST) To: Thomas Hruska Cc: PHP Development In-Reply-To: <5363AF7E.5040100@cubiclesoft.com> References: <5363AF7E.5040100@cubiclesoft.com> Content-Type: text/plain; charset="UTF-8" Date: Fri, 02 May 2014 17:07:51 +0200 Message-ID: <1399043272.5851.26.camel@guybrush> Mime-Version: 1.0 X-Mailer: Evolution 2.30.3 Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Destructor best practice? From: johannes@schlueters.de (Johannes =?ISO-8859-1?Q?Schl=FCter?=) On Fri, 2014-05-02 at 07:45 -0700, Thomas Hruska wrote: > ... > retval.handle = zend_objects_store_put(intern, > (zend_objects_store_dtor_t) zend_objects_destroy_object, > my_free_function_callback, NULL TSRMLS_CC); > > retval.handlers = zend_get_std_object_handlers(); > ... > > In which 'my_free_function_callback' is the callback that frees > underlying structure information. This is guaranteed to be called *exactly* once. > The alternative is to use __destruct: > > PHP_METHOD(my_class, __destruct) > { > } This might be called and arbitrary number of times or might not be called at all. (inherited classes might overwrite the dtor and not call the parent; user might manually call $oject->__destruct() as they like) > Which is considered best-practice? I'm leaning toward the latter, but > want to make sure there aren't any issues with relying on the __destruct > approach. My opinion is that __destruct() should "unset" the object state, as it makes sense, it should exist (unless class is marked final) so users can reliably call it from child classes. __destruct() should *not* destroy internal object structures which leaves pointers around etc. but always leave a defined state. The dtor handler should do all required cleanup to ensure there are no leaks etc. This mostly means cleaning up things the engine can't reach otherwise (having "struct my_object { zend_object inner; my_ptr_t *ptr; }" object->ptr should be cleaned up here) johannes