Skip to content

Instantly share code, notes, and snippets.

@hopeseekr
Last active November 21, 2019 11:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hopeseekr/13809adb6cf8591b2bc6 to your computer and use it in GitHub Desktop.
Save hopeseekr/13809adb6cf8591b2bc6 to your computer and use it in GitHub Desktop.
Avoid PHP Memory Leaks in Long-lived Objects
<?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