PHP单例类终身黑客攻击

时间:2012-02-08 18:52:11

标签: php oop

我有一个非常具体的问题,我无法解决。

我有一个PHP系统,它包含许多类。其中大多数是通用类,使用自动加载处理程序加载,并在全局范围内手动创建(运算符)。

但我也有一个主要且非常重要的单例类,它也存在于全局范围内并且首先创建。我想让它活到最后。必须最后销毁此类(首先创建)。

这个类是一个错误处理类,它只有两个公共方法,用于管理错误,捕获异常,检查退出,发送报告等。

但是,这个类会先破坏,因为它是先创建的。

是否有可能影响班级的生命周期并使其在所有其他班级死亡后死亡?

感谢。

UPD 扩展代码示例:

在分隔文件中定义的每个类

class longlive { // error processing class
    private function __construct() {}

    public static function initialize() {
        self::$instance = new longlive();
    }

    public function __destruct() {
        /// check for runtime session errors, send reports to administrators
    }

    public static function handler( $errno, $errstr, $errfile = '', $errline = '', $errcontext = array() ) {
        /// set_error_handler set this method
        /// process errors and store 
    }

    public static function checkExit() {
        /// register_shutdown_function will register this method
        /// will trigger on normal exit or if exception throwed
        /// show nice screen of death if got an uncoverable error
    }
}

class some_wrapper {
    public function __construct() {}
    public function __destruct() {}
}

class core {
    private function __construct() {
        $this->wrapper = new some_wrapper();
    }

    public static function initialize() {
        self::$core = new core();
    }
}

脚本正文:

include_once( 'path/to/longlive.php' );
longlive::initialize();

include_once( 'path/to/core.php' );
core::initialize();

1 个答案:

答案 0 :(得分:1)

如果您使用Zend Framework,则可以执行此操作:

Yours::initialize();
$application = new Zend_Application(
    APPLICATION_ENV,
    APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap()
            ->run();
unset( $application);
Yours::destroy();

如果您使用自己的代码,则唯一的选择可能是:

Yours::initialize();
runApplication(); // This will contain all other objects in local scope and they
                  // will be destroyed before yours
Yours::destroy();

或使用以下代码破解shutdown handler

foreach( $GLOBALS as $key => $val){
  unset( $GLOBALS[ $key]);
}

Yours::destroy();
相关问题