W3docs

new

The "new" keyword is used in PHP to create new objects from classes. In this article, we will explore the syntax and usage of the "new" keyword in depth, and

The PHP new Keyword

A class is a blueprint; an object (or instance) is a concrete thing built from that blueprint. The new keyword is what builds it — it allocates a fresh object, runs the class's constructor, and hands you back a reference to the result. Almost every time you work with objects in PHP, new is the starting point.

This page covers the syntax of new, what happens when you call it, how constructor arguments work, and the modern variations you'll meet in real code (dynamic class names, new self/new static, anonymous classes, and the parenthesis-free form added in PHP 8.4).

Syntax

$object = new ClassName(arguments);
  • new triggers instantiation.
  • ClassName is the class to instantiate.
  • arguments are passed to the class's __construct() method. If the class has no constructor, or its constructor takes no parameters, you can use empty parentheses new ClassName().

The result is an object handle assigned to $object. Calling new again on the same class produces a separate, independent object.

A first example

<?php

class Greeter
{
  public function sayHello(): void
  {
    echo "Hello!";
  }
}

$object = new Greeter();
$object->sayHello();
// Output: Hello!

Once the object exists, you use the -> operator to call its methods and read its properties.

Passing constructor arguments

If the class defines a __construct() method, the arguments you put in the parentheses after new are forwarded to it. This is how you give each object its own initial state:

<?php

class User
{
  public function __construct(public string $name) {}
}

$alice = new User("Alice");
$bob   = new User("Bob");

echo $alice->name; // Alice
echo "\n";
echo $bob->name;   // Bob

$alice and $bob are two distinct objects of the same class, each holding its own $name. Learn more in PHP Constructor.

Try it Yourself isn't available for this example.

Creating an object from a variable class name

The class to instantiate doesn't have to be hard-coded — it can come from a variable. This is the backbone of factories, plugin systems, and dependency containers:

<?php

class PdfReport {}
class CsvReport {}

$type = "Csv";
$className = $type . "Report"; // "CsvReport"

$report = new $className();
echo get_class($report);
// Output: CsvReport

new self, new static, and new parent

Inside a class you often need to create another instance of the same class. Two keywords do this, and the difference matters when inheritance is involved:

  • new self — always instantiates the class where the line is written.
  • new static — instantiates the actual class at runtime (late static binding), so subclasses get their own type.
<?php

class Animal
{
  public static function create(): static
  {
    return new static();
  }
}

class Dog extends Animal {}

echo get_class(Animal::create()); // Animal
echo "\n";
echo get_class(Dog::create());    // Dog (thanks to `new static`)

If create() had used new self(), both calls would return Animal. See Static Keyword for more on late static binding.

Anonymous classes

When you need a one-off object and don't want to name a class, new class creates and instantiates an anonymous class in a single expression:

<?php

$logger = new class {
  public function log(string $message): void
  {
    echo "LOG: $message";
  }
};

$logger->log("started");
// Output: LOG: started

Anonymous classes can take constructor arguments, implement interfaces, and extend other classes — useful for quick mocks and lightweight callbacks.

Calling a method directly on new (PHP 8.4+)

Historically you had to wrap a new expression in parentheses to call a method on the fresh object. Since PHP 8.4 the parentheses are optional:

// PHP 8.4 and later:
$name = new User("Alice")->name;

// Before PHP 8.4 you had to write:
$name = (new User("Alice"))->name;

Use the parenthesised form if you need to support PHP 8.3 or older.

Common gotchas

  • new returns an object, never a copy. Assigning the object to another variable copies the handle, not the object. To get an independent duplicate, use clone.
  • Missing arguments throw an error. If the constructor declares required parameters and you omit them, PHP throws an ArgumentCountError.
  • Forgetting parentheses on a plain instantiation. new MyClass (no parens) is valid only when no constructor arguments are required; prefer new MyClass() for clarity outside the 8.4 method-chaining case.

Practice

Practice
What is the syntax to initialize a new object in PHP?
What is the syntax to initialize a new object in PHP?
Was this page helpful?