Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Operators

PHP new and clone

Reference for PHP new and clone: instantiate classes, anonymous classes, new in expressions, shallow copies with clone, __clone() and deep copies.

new creates an object: it allocates the instance and runs the class constructor with the arguments you pass. clone creates a second object with copies of the first object's property values. Assigning an object to another variable does not copy it; both variables point to the same object.

A clone is shallow. Scalar properties are copied, but a property that holds another object still points at the same inner object in both copies. If that is not what you want, implement __clone() and copy the inner objects there.

PHP
$a = new Point(1, 2);
$b = $a;          // same object
$c = clone $a;    // new object, same property values

Try it

PHP Handles versus clones

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP Shallow versus deep copies

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
new ClassName(arguments)
new ClassName            // parentheses optional without arguments
new $classNameString(arguments)
new class(arguments) extends Base implements Contract { ... }
new (expression)(arguments)
clone $object
Part Description
ClassName A class that is not abstract, an interface or an enum
arguments Passed to __construct(), positionally or by name
$object The object to copy

Result: new returns the new object. clone returns a new object of the same class.

Errors: new on an abstract class, interface, trait or enum throws Error; an unknown class throws Error ("Class not found"). clone on a non-object throws Error; cloning an enum case, a Generator or a class with a private __clone() is not allowed.

Usage notes

  • new in initializers (PHP 8.1). You can write new in default parameter values, attribute arguments, static variable initializers and constant expressions.
  • Chaining without parentheses (PHP 8.4). new Foo()->method() works; before 8.4 you had to write (new Foo())->method().
  • new with an expression. new (trim($name)) and new $obj (same class as $obj) both work.
  • Constructors can be private to force factory methods or singletons.
  • __clone() runs on the new copy after property values are copied. Use it to deep-copy object properties, reset ids and release shared resources.
  • Readonly properties cannot be modified in __clone() before PHP 8.3; since 8.3 they can be re-initialized inside __clone().
  • Objects in arrays are handles, too. array_map(fn ($o) => clone $o, $list) copies a list of objects.
  • clone is not recursive. For nested data you can also use unserialize(serialize($obj)), which is slower and has its own limits.
  • Comparison: == compares properties, === compares identity. See Comparing objects.