|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2015-08-12 14:20 UTC] raat1979 at gmail dot com
Description: ------------ At the moment the only way (i know of) to determine if a function is called during shutdown by means of a register_shutdown function is to do a debug_backtrace which is a resource heavy function or to enable output buffering and check for PHP_OUTPUT_HANDLER_END which also uses resources for the output buffering It would preferable to have a way to check if the script is in shutdown mode using a more lightweight function. I took an implementation of a static constructor/destructor for a utility class as example but it would be usable for many other purposes https://wiki.php.net/rfc/static_class_constructor considers the implementation of static constructors but also does not see the need for destructors. I agree that there is no need to create a standard implementation for a static destructor. However should you need it it would be really practical to detect that shutdown has started without allocating a lot of resources. Test script: --------------- abstract class utilityClass{ private static $_constructed = false; //seal the constructor to prevent creation even from subclasses final private function __construct(){} final public static function constructStatic(){ if(self::$_constructed!==false) return; self::$_constructed = null; /* do construction here */ self::$_constructed = true; } final public static function destructStatic(){ if(self::$_constructed!==true) return; // I REALLY DON'T LIKE THE BELOW debug_backtrace call if(count(($bt=debug_backtrace()))>1 || isset($bt[0]['file']) || isset($bt[0]['line'])) return; $_constructed = null; /* do destruction here */ $_constructed = false; } }utilityClass::constructStatic(); PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 29 03:00:01 2025 UTC |
I forgot the line "register_shutdown_function(array('utilityClass', 'destructStatic'));" after "self::$_constructed = null;" in the test script constructStatic, will issue a new one later