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);newtriggers instantiation.ClassNameis the class to instantiate.argumentsare passed to the class's__construct()method. If the class has no constructor, or its constructor takes no parameters, you can use empty parenthesesnew 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.
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: CsvReportnew 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: startedAnonymous 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
newreturns 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; prefernew MyClass()for clarity outside the 8.4 method-chaining case.
Related topics
- PHP Classes and Objects — the foundation
newbuilds on. - PHP Constructor — how arguments to
neware received. - clone — duplicating an object you already created.
- Static Keyword —
new staticand late static binding.