Fixes #2869

Ported https://bugs.php.net/bug.php?id=66021.