Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

PHP (Plugin)

The PHP emitter generates PHP 8.1+ readonly classes with constructor-promoted properties.

Note: This is a plugin emitter. It must be built and installed as a shared library before use.

Quick Start

#![allow(unused)]
fn main() {
#[derive(TypeWriter)]
#[sync_to(php)]
pub struct User {
    pub id: String,
    pub name: String,
    pub age: Option<u32>,
}
}

Output (User.php):

<?php
declare(strict_types=1);

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

Type Mappings

RustPHP
Stringstring
boolbool
u32, i64, etc.int
f64float
Option<T>?T
Vec<T>array
HashMap<K, V>array
Uuidstring
DateTime\DateTimeInterface

Enum Mapping

  • Unit enumsenum Role: string (PHP 8.1 backed enum)
  • Complex enumsinterface + readonly class implementations

Configuration

[php]
output_dir = "./generated/php"
file_style = "PascalCase"

See the full PHP Emitter documentation for more details.