Creating anonymous objects in php

Creating anonymous objects in php

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Creating Anonymous Objects in PHP

JavaScript developers are accustomed to creating anonymous objects instantly using object literals, as seen in the standard syntax where properties and arrays are defined inline. You can define a property p holding a value and an array p1 containing names like "john" and "johnny", then immediately access object.p1[1] to retrieve "johnny". This pattern relies on the language's dynamic nature, allowing you to construct data structures without pre-declaring their class or type.

In PHP, however, this specific syntax does not exist. You cannot write { p: "value" } and expect it to instantiate an object. PHP treats curly braces differently; they are primarily used for array construction, not object instantiation. Attempting to assign an anonymous block of key-value pairs directly to a variable without a class definition will result in a syntax error or create an associative array rather than an object.

To achieve similar functionality in PHP, you must define a class. Even if the class is never intended to be used elsewhere and exists solely for that specific instance, the language requires a declaration block. A minimal anonymous-like object looks like this:

$object = new stdClass();
$object->p = "value";
$object->p1 = ["john", "johnny"];

echo $object->p1[1]; // Outputs: johnny

Using stdClass is the standard approach for creating objects that require no specific functionality beyond data storage. This class is part of the core PHP installation, so you do not need to install any additional packages or extend external libraries like those found on LaravelCompany unless you are building a custom framework that requires dynamic property access traits.

While stdClass works perfectly for simple scenarios, it lacks compile-time type safety. Modern PHP development often favors strict typing to catch errors early. If you are working within a typed context, using the #[\AllowDynamicProperties] attribute on a dedicated class can provide a middle ground, though this is generally discouraged in favor of explicit property declarations.

class User {
    #[\AllowDynamicProperties] // Rarely needed with strict typing
    public string $p;
    public array $p1;
}

$user = new User();
$user->p = "value";
$user->p1 = ["john", "johnny"];

The most significant trade-off here is the verbosity. The JavaScript approach allows for rapid prototyping and concise data structures in a single line, which appeals to developers coming from that ecosystem. PHP forces you to be more explicit about structure. However, this explicitness provides benefits: IDE autocomplete works immediately on defined properties, and tools like Laravel's Eloquent ORM can interact with these structures if they adhere to naming conventions or use castable attributes.

If your goal is simply to pass a JSON-like structure around without creating an object instance, PHP offers json_decode. However, this returns an array by default unless you specify the second argument as true to get an object. This distinction is crucial because mixing arrays and objects can lead to subtle bugs when accessing data via $data['key'] versus $data->key.

Ultimately, while PHP cannot replicate the syntactic sugar of JavaScript's anonymous objects, stdClass serves as the direct equivalent for runtime dynamic objects. It offers the flexibility you need without sacrificing the robustness that static typing and core language features provide.

Our network