Created
October 8, 2021 07:40
-
-
Save 24HOURSMEDIA/c5dbef581e14e64a7dd79ad2b98b36f9 to your computer and use it in GitHub Desktop.
test php deferred destruction when callbacks are injected
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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