Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

new On Object

<?php

class X {}

$s = '\x'; new $s;       // a x object
$x = new x(); new $x();  // another x object

$s = x::class;
$a = new $s;            // yet another one

// syntax error, unexpected token "class",
// expecting variable or "$s"
$a = new x::class;

?>

It is possible to use new on an object, to get a new object of the same class. This might be useful with anonymous classes, which cannot be created with their name.

When using the parenthesis after the variable, the __invoke() magic method won’t be called, as those parenthesis are used to call the constructor. To get that result, it must be called outside the new command or within parenthesis, as new ($a()).

Under the hood, PHP collects the class name from the object to produce the new one. It is akin to using the class name in a string.

Lastly, the full class name is available in the x::class syntax, but PHP won’t recognize new x::class, and even emit a Fatal error: it recognizes new (x::class).

See Also

PHP Features

Last updated: 10 September 2026