ds261634878 2017-01-18 21:15
浏览 140
已采纳

在销毁所有对象后调用PHP函数

I have seen several answers on object destruction order, and all point out that order is not guaranteed. Since I cannot control the order, I would like to invoke a function after all objects have been destroyed.

register_shutdown_function is invoked prior to object destruction, thus not an option. I have looked at tricks like set_error_handler using the object so it is invoked "late", but that is not sufficient.

Some background on the problem, this is a complex CMS with dozens of separate files for routes (view) layer. There is a common boot include, but not a common one run at shutdown. I am using APCu object caching through a common inherited base class, and need to make sure an object is purged. It is possible that for any two instances of the same object created during a page load, one might want to purge itself, and the other might want to cache itself. Obviously purge trumps all else, so I need to call apc_delete on a global set of cache keys to purge one all __destruct()'ion is complete.

  • 写回答

2条回答 默认 最新

  • douchigu1723 2017-01-20 16:38
    关注

    As I said in my comment above, the mixed state of multiple object instances sounds like a design flaw. I think you always want all instances of an object to be the latest, or at least not touch the cache if they aren't. I would think your caching and/or object loading layer could handle this.


    To answer your underlying question, I wrote a script to test PHP's shutdown sequence:

    <?php  /* Adapted from https://evertpot.com/160/ */
    
        ob_start(
            function($buffer) {
                return $buffer . "output buffer flushed
    ";
            }
        );
    
        $empty = function() {
            return true;
        };
    
        $close = function() {
            echo "session close
    ";
            return true;
        };
    
        $write = function() {
            echo "session write
    ";
            return true;
        };
    
        session_set_save_handler($empty, $close, $empty, $write, $empty, $empty);
        session_start();
    
        register_shutdown_function(
            function() {
                echo "register_shutdown_function
    ";
            }
        );
    
        class MyClass {
            function __destruct() {
               echo "object destructor
    ";
            }
        }
        $myObject = new MyClass;
    

    The output:

    register_shutdown_function
    object destructor
    output buffer flushed
    session write
    session close
    

    It seems the flush of output buffering and session write/close are 2 places where you know all of your objects have been destroyed. (This also assumes you're not using the newer form of session_set_save_handler that can register itself as a shutdown function.)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?