__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;
?>
Alternatives
- Check the data before cloning it, with is_object() or instanceof.
Related error messages
In more recent PHP versions, this error message is now :ref:clone():-argument-#1-($object)-must-be-of-type-object,-%s-given.