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
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
24 + 15 = ?
Subscribe to this entry?

 
 [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

Add a Patch

Pull Requests

Add a Pull Request

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-2020 The PHP Group
All rights reserved.
Last updated: Mon Mar 30 10:01:26 2020 UTC