Constructor Property Promotion

Who writes the boilerplate for you — PHP 8.0 constructor promotion against parameter-to-field shortcuts in 10 languages.

C C23

C23
#include <stdio.h>

/* C has no constructors: the idiom is an init function the caller must
   remember to run — nothing enforces it */
typedef struct {
    const char *name;
    int age;
} User;

static void user_init(User *u, const char *name, int age) {
    u->name = name;
    u->age = age;
}

int main(void) {
    User u;
    user_init(&u, "Ada", 36);
    printf("%s (%d)\n", u.name, u.age);
    return 0;
}
Output
Ada (36)

C++ C++26

C++26
#include <cstdio>
#include <string>
#include <utility>

class User {
public:
    // the initializer list does the wiring; C++ lacks a promotion shorthand
    User(std::string name, int age) : name_(std::move(name)), age_(age) {}
    const std::string &name() const { return name_; }
    int age() const { return age_; }

private:
    std::string name_;
    int age_;
};

int main() {
    User u{"Ada", 36};
    std::printf("%s (%d)\n", u.name().c_str(), u.age());
    return 0;
}
Output
Ada (36)

C# v11.0.100

v11.0.100
// primary constructor (C# 12): parameters live in the type header; they are
// plain parameters, not auto-created properties
Console.WriteLine(new User(36, "Ada").Describe());

// to expose values as properties you promote them yourself
Console.WriteLine(new Box("payload").Label);

class User(int age, string name)
{
    public string Describe() => $"{name} ({age})";
}

class Box(string label)
{
    public string Label { get; } = label; // property initialized from the parameter
}
Output
Ada (36)
payload

Go v1.27.4

v1.27.4
package main

import "fmt"

// Go has no constructor syntax; the idiom is a New* factory function
type User struct {
	name string
	age  int
}

func NewUser(name string, age int) *User {
	return &User{name: name, age: age} // composite literal does the wiring
}

func (u *User) Describe() string { return fmt.Sprintf("%s (%d)", u.name, u.age) }

func main() {
	u := NewUser("Ada", 36)
	fmt.Println(u.Describe())
}
Output
Ada (36)

Java v27.0.1

v27.0.1
public class Main {
    // a record IS the promoted boilerplate: the header declares the fields,
    // accessors, equals, hashCode, toString — all generated
    record User(String name, int age) {
        User {
            if (age < 0) throw new IllegalArgumentException("age");
        }
    }

    public static void main(String[] args) {
        User u = new User("Ada", 36);
        System.out.println(u.name() + " (" + u.age() + ")");
    }
}
Output
Ada (36)

JavaScript v26.13.1

v26.13.1
// JS has no promotion: every field needs an explicit assignment line
class User {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }
  describe() { return `${this.name} (${this.age})`; }
}
console.log(new User("Ada", 36).describe());
Output
Ada (36)

PHP v8.6.0

v8.6.0
class User {
    // a visibility modifier promotes the parameter to a property
    public function __construct(
        public string $name,
        private int $age = 0,
    ) {}

    public function describe(): string {
        return "{$this->name} ({$this->age})";
    }
}

$u = new User("Ada", 36);
echo $u->describe(), "\n";
echo $u->name, "\n";

// promotion mixes with plain parameters
class Box {
    public function __construct(
        public string $value = "",
        string $label = "box",   // not promoted: parameter only
    ) {
        if ($value === "") { $this->value = $label; }
    }
}
echo (new Box())->value, "\n";
echo (new Box("payload"))->value, "\n";
Output
Ada (36)
Ada
box
payload

Python v3.15.2

v3.15.2
from dataclasses import dataclass

@dataclass
class User:
    # the decorator writes __init__, __repr__ and __eq__ from the annotations
    name: str
    age: int = 0

u = User("Ada", 36)
print(u.name, u.age)
print(repr(u))
Output
Ada 36
User(name='Ada', age=36)

Rust v1.99.1

v1.99.1
struct User {
    name: String,
    age: i32,
}

impl User {
    // the idiomatic constructor; field init shorthand mirrors the parameters
    fn new(name: &str, age: i32) -> Self {
        Self { name: name.to_string(), age }
    }
}

fn main() {
    let u = User::new("Ada", 36);
    println!("{} ({})", u.name, u.age);
}
Output
Ada (36)

TypeScript v26.13.1

v26.13.1
// TS parameter properties (constructor(public name: string)) generate field
// assignments in transpiled output; Node's type stripping rejects them, so
// the equivalent explicit form is shown
class User {
  name: string;
  age: number;
  constructor(name: string, age: number) {
    this.name = name;
    this.age = age;
  }
  describe(): string { return `${this.name} (${this.age})`; }
}
console.log(new User("Ada", 36).describe());
Output
Ada (36)

Language differences

C: no constructors (example): the idiom is an init function the caller must remember to run, enforced by nothing; field wiring is all manual.

C++: the member initializer list (example): wiring happens in the constructor but there is no parameter-as-field shorthand; aggregate initialization since C++20 trims some boilerplate, yet members still need in-class declarations.

C#: the primary constructor, C# 12 (example): parameters sit in the type header and are usable in members — but they are plain parameters and do not become properties automatically; exposing one needs your own { get; } = label. The PHP contrast: a single modifier declares property and parameter in one step.

Go: no constructor syntax (example): the idiom is a New* factory function plus a composite literal doing the wiring; export status is decided by field-name casing, unrelated to construction.

Java: the record (16+, example): semantically the closest to PHP promotion — header parameters become final fields plus accessors plus a generated equals/hashCode/toString set; the compact constructor can validate, but the fields carry final immutability semantics.

JavaScript: no promotion (example): every field costs one explicit this.x = x line; class-field syntax declares defaults, but parameter-to-field still hand-written.

PHP: 8.0+ constructor promotion (example): a single modifier — public/protected/private or readonly — triggers it, declaring parameter and property in one step, with the body free for extra logic; promoted and plain parameters mix freely; promoted parameters cannot be typed callable, and attributes on them replicate to both property and parameter.

Python: @dataclass (example): the decorator generates __init__/__repr__/__eq__ from class annotations; defaults live on the fields — semantics are declarative generation rather than promotion, but the boilerplate removed is the same.

Rust: the idiomatic new() (example): no built-in constructors, and the Self { name, age } field-init shorthand lets same-named parameters flow straight in; construction moves ownership.

TypeScript: parameter properties (example): constructor(public name: string) generates the assignments in transpiled output — the precursor of PHP's promotion; but it is transpile-time codegen that Node's native stripping rejects, so the example shows the equivalent explicit assignments (see notes).