Registering a static class method as a shutdown function in PHP

In PHP, can I 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(); } } 
+5
source share
3 answers

The first argument passed to register_shutdown_function is of type callable . The called static method is as follows:

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

As with PHP 5.2.3, it may also look exactly like you,

+9
source

May use an anonymous function:

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

In addition, in the analyzer you can use a call of the same class as this:

 class Analyzer{ public static function log_shutdown(){ self::screenshot(); } } 
+6
source

In the end, through the namespace:

 register_shutdown_function(function(){ \app\modules\Analyzer\Analyzer::log_shutdown(); }); 

or

 register_shutdown_function(['\app\modules\Analyzer\Analyzer', 'log_shutdown']); register_shutdown_function(["\\app\\modules\\Analyzer\\Analyzer", "log_shutdown"]); 
0
source

Source: https://habr.com/ru/post/1234093/


All Articles