Skip to content

Instantly share code, notes, and snippets.

@24HOURSMEDIA
Created October 8, 2021 07:40
Show Gist options
  • Save 24HOURSMEDIA/c5dbef581e14e64a7dd79ad2b98b36f9 to your computer and use it in GitHub Desktop.
Save 24HOURSMEDIA/c5dbef581e14e64a7dd79ad2b98b36f9 to your computer and use it in GitHub Desktop.
test php deferred destruction when callbacks are injected
output:
no callback: testing...destruct...
with callback: testing...callback...destruct...
with callback 2: testing...callback...destruct...was unset...
(destruct was always called so that's ok)
<?php
class A {
protected $cb;
public function set($cb) {
$this->cb = $cb;
}
public function test() {
echo 'testing...';
$cb = $this->cb;
$cb && $cb();
}
public function __destruct() {
echo 'destruct...';
}
}
function test() {
$a = new A();
$a->test();
}
function testCB() {
$a = new A();
$a->set(function() {
echo 'callback...';
});
$a->test();
}
function testCB2() {
$a = new A();
$a->set(function() {
echo 'callback...';
});
$a->test();
unset($a);
echo "was unset...";
}
echo 'no callback: ';
test();
echo PHP_EOL;
echo 'with callback: ';
testCB();
echo PHP_EOL;
echo 'with callback 2: ';
testCB2();
echo PHP_EOL;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment