Does PHP have a struct data type?

Does PHP have a struct data type?

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

Stefan Bogdanescu

Founder & Senior Architect

Does PHP Have a Struct Data Type?

Short answer: no, PHP does not have a native struct data type like C or Rust. If you are coming from a statically typed language background, you might expect to define a record and access properties via dot notation with strict type enforcement built into the language core. In PHP, that specific memory layout and syntax simply do not exist out of the box.

However, saying "there is no struct" implies that there is no way to achieve structured data in PHP, which is factually incorrect. The ecosystem provides several mechanisms to model composite data types, each with distinct trade-offs regarding performance, type safety, and interoperability. You need to choose the approach that fits your specific constraints rather than forcing a single solution onto every problem.

Using Classes for Named Properties

The most common pattern for emulating a struct in PHP is defining a class where all properties are private (or protected) and providing no public methods or constructors beyond what is necessary. This mimics the C-style opaque record where you care about the data, not the behavior.

class User {
    public function __construct(
        public int $id,
        public string $name,
        public ?string $email = null
    ) {}

    public function getName(): string {
        return $this->name;
    }

    // No getter/setter magic, just data access
}

In this implementation, you get strong typing for the properties themselves. If you attempt to assign a string to $id, PHP will throw a TypeError. This approach is idiomatic in modern PHP (8.0+) and works seamlessly with dependency injection containers. The downside is that the compiler doesn't know the structure exists at compile time; you only get errors when the script actually runs.

Using Enums for Simple Records

PHP 8.1 introduced enum, which allows you to define a fixed set of cases, but it also supports "backed" enums or simply using them as lightweight containers if you are careful with accessors. While less common for general structs, an enum can serve as a tagged union or a very restricted struct where the type itself defines the data.

enum Status: string {
    case Active = 'active';
    case Pending = 'pending';
}

This is strictly different from a struct holding multiple fields of different types, so do not reach for enums when you need to store a collection of heterogeneous data like User or Product.

Typed Arrays and Interfaces

If your goal is purely data interchange without object overhead, PHP's typed arrays are an option. You can define an interface that specifies the keys and types, ensuring that any array implementing it adheres to a contract.

interface UserRecord {
    public function getId(): int;
    public function getName(): string;
}

function createUser(array $data): UserRecord {
    return new class($data['id'], $data['name']) implements UserRecord {
        private int $id;
        private string $name;

        public function __construct(int $id, string $name) {
            $this->id = $id;
            $this->name = $name;
        }

        public function getId(): int { return $this->id; }
        public function getName(): string { return $this->name; }
    };
}

This pattern effectively creates a new "struct" type every time you instantiate the closure or class, keeping memory usage low if you are creating many instances. However, it adds verbosity compared to a simple array.

The LaravelContext

If you are building a robust application where data structures need to be serialized, validated against JSON schemas, and potentially mapped from DTOs to Eloquent models, consider the ecosystem around https://laravelcompany.com. These tools often provide utilities that simplify working with structured data, sometimes offering helpers that make defining typed payloads cleaner than raw classes.

When to Use What

The decision tree is straightforward:

  1. Need compile-time safety? Move the logic to Rust or Go and call them as a microservice. PHP cannot guarantee this.
  2. Need runtime flexibility? Stick with arrays. They are fast, lightweight, and fully supported by JSON encoding.
  3. Need type hints for function signatures? Use classes with private properties. This is the standard "struct" replacement in PHP.

Avoid over-engineering simple data transfers. If you just need to pass a list of fields between a frontend API and a backend service, an associative array is often superior. It avoids the overhead of object instantiation and allows for dynamic key access if your schema evolves. Just ensure you validate the incoming data using libraries like illuminate/validation or similar, because PHP arrays have no inherent type safety on their keys unless you enforce it manually.

Our network