Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Cloning Static Variable

<?php

// First class callable
function a() { static $c = 0; return $c++; }
$c = a(...);

$c = function () { static $c = 0; return $c++; };

print $c();
print $c();
$d = clone $c;
print $d();

?>

It is not possible to clone a static variable, but it is well possible to duplicate a closure which contains a static variable. Then, the variable is duplicated with its content at the time of cloning. So, it might be the default value, or it might be an subsequent value if the closure has already been used.

When the original closure is a first class callable, the moment of cloning does not matter.

See Also

PHP Features

Last updated: 10 September 2026