php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #70248 lighweight function to detect that shutdown has started
Submitted: 2015-08-12 14:20 UTC Modified: 2015-08-12 14:49 UTC
From: raat1979 at gmail dot com Assigned:
Status: Closed Package: PHP options/info functions
PHP Version: Irrelevant OS: Irellevant
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: raat1979 at gmail dot com
New email:
PHP Version: OS:

 

 [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();


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-08-12 14:49 UTC] raat1979 at gmail dot com
-Status: Open +Status: Closed
 [2015-08-12 14:49 UTC] raat1979 at gmail dot com
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
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jul 02 05:01:42 2025 UTC