1.2.149. Clone With Non-Object

The clone keyword must be used on variables, properties or results from a function or method call.

clone cannot be used with constants or literals. Cloning a non-object lint but won’t execute.

<?php

class x { }
$x = new x();

// Valid clone
$y = clone $x;

// Invalid clone
$y = clone x;

?>

See also Object cloning.

1.2.149.1. Suggestions

  • Only clone containers (like variables, properties…)

  • Add typehint to injected properties, so they are checked as objects.

1.2.149.2. Specs

Short name

Classes/CloneWithNonObject

Rulesets

All, Analyze, LintButWontExec

Exakat since

1.7.0

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

High

Features

clone

Note

This issue may lint but will not run

Available in

Entreprise Edition, Exakat Cloud