__clone method called on non-object

Description

This error signals an attempt at cloning a value that is not an object. Indeed, anything but an object triggers this error: boolean, array, string, etc.

Note that it is possible to clone a constant since PHP 8.1, so it is legit to use clone on a constant.

Example

<?php

$a = clone array();

$b = null;
clone null;

const C = new Stdclass();
clone C;

?>

Solutions

  • Check the data before cloning it, with is_object() or instanceof.