PHP register_shutdown_function alternative

  • joebert
  • Genius
  • Genius
  • User avatar
  • Posts: 13511
  • Loc: Florida

Post 3+ Months Ago

I hadn't realized until today that PHP actually has de-constructors for objects, which sucks for me, but it also just happens to be the perfect time for me to discover them. Rather than fool around with register_shutdown_function and worry about whether something I'm not even sure I read a few years ago about some servers having problems with that function is even the way it is.

In any event, the __destruct magic method counterpart to __construct is easier to use than register_shutdown_function anyways, if you ask me. The thing that kinda sucks though, is that the use of __destruct requires you to have references to the constructed objects lingering around until the end of the scripts execution.

You can use a static array within the class to save these references though.

Anywho, here's an example. I use this to store references to cached HTML files that need to be deleted.

PHP Code: [ Select ]
class garbage
   protected static $can = array();
   public static function collect($mask)
      array_push(garbage::$can, new garbage($mask));
   private $mask;
   public function __construct($mask)
      $this->mask = $mask;
   public function __destruct()
  1. <?php
  3. class garbage
  4. {
  5.    protected static $can = array();
  6.    public static function collect($mask)
  7.    {
  8.       array_push(garbage::$can, new garbage($mask));
  9.    }
  11.    private $mask;
  12.    public function __construct($mask)
  13.    {
  14.       $this->mask = $mask;
  15.    }
  16.    public function __destruct()
  17.    {
  18.       file_utils::glob_delete($this->mask);
  19.    }
  20. }
  22. ?>

Post Information

  • Total Posts in this topic: 1 post
  • Users browsing this forum: No registered users and 34 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum

© 1998-2017. Ozzu® is a registered trademark of Unmelted, LLC.