note 105300 deleted from language.oop5.static by cmb

From: Date: Sun, 01 Sep 2019 07:15:36 +0000
Subject: note 105300 deleted from language.oop5.static by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-212041@lists.php.net to get a copy of this message
Note Submitter: programmer-comfreek at hotmail dot com ---- If you haven't an instance for your class (e.g. all functions are static), but you also want a __destruct() functionality, consider the following example: We have a class which loads and saves data so we also want to have an autosave mechanism which is called at the end of the PHP script. So usually you declare a __destruct function but our class is designed to provide static functions / variables instead: <?php class A { static private $autoSave; static public function init($autoSave) { /* emulating __construct() */ self::$autoSave = $autoSave; } static public function save() { /*...*/ } /* load(), get(), etc. */ } ?> In order to define a __destruct function (which is definitely called) we create a new instance in the init() function and define a destruct() function (which is called from the 'real' one): <?php class B { static private $autoSave; static public function init($autoSave) { /* emulating __construct() */ self::$autoSave = $autoSave; new B(); } static public function destruct() { if (self::$autoSave) self::save(); } public function __destruct() { B::destruct(); } } ?>

« previous php.notes (#212041) next »