Trying to clone an uncloneable object of class %s

Description

It is not possible to instantiate certain classes. This is the case of enumerations and generators.

It is also not possible to clone an enumeration cases, which are actually of the same type as the enumeration itself. The only usage is to access the cases of the enumeration: they are directly the expected objects.

On the other hand, it is possible to clone a closure.

Example

<?php

enum E {
    case A;
}
clone E::A;

$g = function () { yield 1; };
clone $g;

$c = function () { return 1; };
clone $c;

?>

Literal Examples

  • Trying to clone an uncloneable object of class e

Solutions

  • Use the case E::A. Make sure it is not a class constant, which might be cloned.

  • For generator, call the original variable anytime the code needs a new source for a loop.