Documentation · Chapter 09

Make PHP state reactive.

A #[State] property is serialized into the document and becomes observable by Engine. Supported closures are compiled into browser instructions.

Every lesson starts from the problem, explains the mechanism, then gives a concrete verification.

Read and change ordinary properties

Inside a supported onClick closure, $this->count++ is compiled rather than executed on the server at render time. Only #[State] properties may participate.

phpaml — zsh
#[State] private int $count = 0;
#[State] private string $name = '';

Button('Add')->onClick(fn () => $this->count++);
TextField()->value($this->state('name'));

Bind form fields through value()

TextField()->value($this->state('name')) creates two-way binding. name() remains the HTML submission name; value() controls the reactive value.

Use effects for reactions, not derived display

Use normal PHP expressions for values derived during body(). Use Effect only for a side effect such as logging, timers, storage, or an API request.