Last active
November 21, 2019 11:47
-
-
Save hopeseekr/13809adb6cf8591b2bc6 to your computer and use it in GitHub Desktop.
Avoid PHP Memory Leaks in Long-lived Objects
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 | |
// Avoid PHP Memory Leaks in Long-lived Objects | |
// License: Public Domain | |
class LongLivedObject | |
{ | |
public function __construct() { | |
echo "Object " . spl_object_hash($this) . " was just born!\n"; | |
} | |
public function __destruct() { | |
echo "Object " . spl_object_hash($this) . " just died.\n"; | |
} | |
} | |
$obj = new LongLivedObject; | |
function a($o) { | |
return clone $o; | |
} | |
$obj2 = a(clone $obj); | |
unset($obj); | |
echo "Filler...\n"; | |
// Object 0000000046d314f90000000034b03b6a was just born! | |
// Object 0000000046d314fa0000000034b03b6a just died. | |
// Object 0000000046d314f90000000034b03b6a just died. | |
// Filler... | |
// Object 0000000046d314fb0000000034b03b6a just died. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment