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
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()
{
file_utils::glob_delete($this->mask);
}
}
?>
- <?php
-
- 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()
- {
- file_utils::glob_delete($this->mask);
- }
- }
-
- ?>
Strong with this one, the sudo is.