← All results

PHP 8.5 clone($object, array $withProperties) copies an object and writes the given properties on the copy

Group: Object compatibility · Category: language · File: objects_clone_with_properties.php

The result is the same class as the original. Literal keys must name an accessible property, and each value must match that property's type. The unary clone $object form is the control: a diagnostic there means the reaction is about cloning in general, not the two-argument form. Source: vimeo/psalm#11893 (6.19.0). References: - https://wiki.php.net/rfc/clone_with_v2 - psalm#11893

Analyzer results

AnalyzerVersionResultDiagnostics
phan 6.0.10Phan 6.0.10 php-ast version 1.1.3 PHP version used to run Phan: 8.5.11 Fail

No diagnostics reported.

Expectation diff
Line 49: Expected 1 error(s)
Line 50: Expected 1 error(s)
phpstan 2.3.1PHPStan - PHP Static Analysis Tool 2.3.1 Pass reported Lv.2+
objects_clone_with_properties.php:49: Access to an undefined property Conformance\Tests\ObjectsCloneWithProperties\Point::$nope. [reported-from-level=2]
objects_clone_with_properties.php:50: Property Conformance\Tests\ObjectsCloneWithProperties\Point::$x (int) does not accept string. [reported-from-level=3]
With strict-rules
objects_clone_with_properties.php:49: Access to an undefined property Conformance\Tests\ObjectsCloneWithProperties\Point::$nope.
objects_clone_with_properties.php:50: Property Conformance\Tests\ObjectsCloneWithProperties\Point::$x (int) does not accept string.
psalm 6.20.0Psalm 6.20.0@804f03c0c183d149fcd4b8c7c0c9b3fbf836d34c
next: 7.0.0-rc2
Pass (pzoom≠)
objects_clone_with_properties.php:49: Instance property Conformance\Tests\ObjectsCloneWithProperties\Point::$nope is not defined [UndefinedPropertyAssignment]
objects_clone_with_properties.php:50: Conformance\Tests\ObjectsCloneWithProperties\Point::$x with declared type 'int' cannot be assigned type ''str'' [InvalidPropertyAssignmentValue]
pzoom (Psalm port)
objects_clone_with_properties.php:46: Argument 1 of takesPoint expects Conformance\Tests\ObjectsCloneWithProperties\Point, but parent type object provided [ArgumentTypeCoercion]
objects_clone_with_properties.php:47: Argument 1 of takesInt cannot be mixed, expecting int [MixedArgument]
mago 1.57.0mago 1.57.0 Fail

No diagnostics reported.

Expectation diff
Line 49: Expected 1 error(s)
Line 50: Expected 1 error(s)
mir 0.87.0mir 0.87.0 Fail

No diagnostics reported.

Expectation diff
Line 49: Expected 1 error(s)
Line 50: Expected 1 error(s)
phpantom 0.11.1phpantom_lsp 0.11.1 Fail

No diagnostics reported.

Expectation diff
Line 49: Expected 1 error(s)
Line 50: Expected 1 error(s)
intelephense 1.18.5intelephense 1.18.5 Fail

No diagnostics reported.

Expectation diff
Line 49: Expected 1 error(s)
Line 50: Expected 1 error(s)
phpactor 2026.10.07.0Phpactor 2026.10.07.0 Fail

No diagnostics reported.

Expectation diff
Line 49: Expected 1 error(s)
Line 50: Expected 1 error(s)
phpy 1.0.19409phpy 1.0.19409 Fail

No diagnostics reported.

Expectation diff
Line 49: Expected 1 error(s)
Line 50: Expected 1 error(s)
qodana 262.10968.76PhpStorm 262.10968.76 Fail

No diagnostics reported.

Expectation diff
Line 49: Expected 1 error(s)
Line 50: Expected 1 error(s)
noverify 0.5.5NoVerify, version 0.5.5: built on: 2025.04.22 14:36:46 OS: r-kuchinskas arm64 Commit: 4774b82f8adc53fbef38f95e97af4b953b4d1529 Fail

No diagnostics reported.

Expectation diff
Line 49: Expected 1 error(s)
Line 50: Expected 1 error(s)
steins 0.1.8steins 0.1.8 (2026-09-21 revision 76306a9) Fail

No diagnostics reported.

Expectation diff
Line 49: Expected 1 error(s)
Line 50: Expected 1 error(s)
phpstan-strict 2.3.1PHPStan - PHP Static Analysis Tool 2.3.1 Pass
objects_clone_with_properties.php:49: Access to an undefined property Conformance\Tests\ObjectsCloneWithProperties\Point::$nope.
objects_clone_with_properties.php:50: Property Conformance\Tests\ObjectsCloneWithProperties\Point::$x (int) does not accept string.
psalm-next 7.0.0-rc2Psalm 7.0.0-rc2@20339e3fe5b8d03bcd8061030eb2a83ad50e5783 Pass
objects_clone_with_properties.php:49: Instance property Conformance\Tests\ObjectsCloneWithProperties\Point::$nope is not defined [UndefinedPropertyAssignment]
objects_clone_with_properties.php:50: Conformance\Tests\ObjectsCloneWithProperties\Point::$x with declared type 'int' cannot be assigned type ''str'' [InvalidPropertyAssignmentValue]

Source

<?php

declare(strict_types=1);

namespace Conformance\Tests\ObjectsCloneWithProperties;

/**
 * PHP 8.5 `clone($object, array $withProperties)` copies an object and writes
 * the given properties on the copy.
 *
 * The result is the same class as the original. Literal keys must name an
 * accessible property, and each value must match that property's type. The
 * unary `clone $object` form is the control: a diagnostic there means the
 * reaction is about cloning in general, not the two-argument form.
 *
 * Source: vimeo/psalm#11893 (6.19.0).
 *
 * References:
 * - https://wiki.php.net/rfc/clone_with_v2
 * - https://github.com/vimeo/psalm/pull/11893
 */

final class Point
{
    public function __construct(
        public int $x,
        public string $label,
    ) {
    }
}

function takesPoint(Point $point): void
{
}

function takesInt(int $value): void
{
}

$point = new Point(1, 'origin');

$unary = clone $point;
takesPoint($unary); // V: unary clone keeps the class

$copy = clone($point, ['x' => 2]);
takesPoint($copy); // V: the two-argument form keeps the class
takesInt($copy->x); // V

clone($point, ['nope' => 1]); // E: Point has no $nope
clone($point, ['x' => 'str']); // E: $x is int, not string