Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Register static class method as shutdown function in PHP

Tags:

php

In PHP is it possible to register a shutdown function (with register_shutdown_function()) which is a static method implemented in a class? I mean something like this:

//index.php
require_once("modules/Analyzer/Analyzer.php");
register_shutdown_function("Analyzer::log_shutdown");

//Analyzer.php
class Analyzer {
   ...

   public static function log_shutdown(){
      // do some awesome stuff
      Analyzer::screenshot();
   }
}
like image 703
ACs Avatar asked Oct 20 '15 08:10

ACs


2 Answers

The first argument passed to register_shutdown_function is of the type callable. A callable static method looks like this:

register_shutdown_function(array('Analyzer', 'log_shutdown')); 

As of PHP 5.2.3 it may also look exactly like what you originally posted

like image 60
Sjon Avatar answered Nov 16 '22 06:11

Sjon


Can use an anonymous function:

register_shutdown_function(function(){
    Analyzer::log_shutdown();
});

also, in analyzer you can use a call to the same class like this:

class Analyzer{
    public static function log_shutdown(){
        self::screenshot();
    }
}
like image 40
Tanuel Mategi Avatar answered Nov 16 '22 06:11

Tanuel Mategi