1

I have a very specific issue which I can't solve.

I have a PHP system which consist of a number of classes. Most of them are generic classes and are loaded by using autoload handler, and created in a global scope manually ( new operator ).

But I also have one main and very important singleton class, which also exists in a global scope and is created first. I want to make it live till the end. This class ( created first ) must be destroyed last.

This class is a error-processing class, and it have only two public methods which will be used to manage errors, catch exceptions, check exit, send reports, etc.

But, this class will destroy first as it was created first.

Is it possible to affect the class's lifetime and make it die after all other classes have died?

Thanks.

upd extended code samples:

each class defined in separated file

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();
    }
}

script body:

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

include_once( 'path/to/core.php' );
core::initialize();
4
  • 5
    "Security reasons" sounds exceedingly unlikely... And I suggest you look at the concepts behind dependency injection: they'll make your coding much easier and your code much more flexible. Commented Feb 8, 2012 at 18:56
  • Security by Singleton? Really?! Commented Feb 8, 2012 at 19:23
  • can you please forget about undisclosed security reasons and consult me about class life cycle? thanks. Commented Feb 8, 2012 at 19:24
  • Can you move the "global" objects into a context of it's own you could unset at the end of your script before PHP shuts down? Commented Feb 8, 2012 at 19:53

1 Answer 1

1

If you were using Zend Framework you could do this:

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

If you're using your own code your only option is probably:

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

Or hack shutdown handler with code like this:

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

Yours::destroy();
Sign up to request clarification or add additional context in comments.

3 Comments

I can't strictly create class instance in a global scope, it will initialize inside itself, see sample code.
@ntvf fixed for static model .)
thank you and sorry, I give not full description. I can do nothing with shutdown handler to keep this one live, see updated samples.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.