Skip to main content
Creates a reference to a runtime variable that will be provided when executing the workflow.

Signature

function variable(string $name): VariableInterface

Parameters

name
string
required
The name of the variable. This must match the named argument passed to run().

Returns

VariableInterface
VariableInterface
A variable reference that will be resolved at runtime.

Usage

Basic variable usage

use function Chevere\Workflow\{workflow, sync, variable, run};

$workflow = workflow(
    greet: sync(
        fn(string $name): string => "Hello, {$name}!",
        name: variable('username')
    )
);

// Provide the variable value when running
$result = run($workflow, username: 'World');
echo $result->response('greet')->string();
// Output: Hello, World!

Multiple variables

$workflow = workflow(
    calculate: sync(
        function (int $a, int $b): int {
            return $a + $b;
        },
        a: 10,
        b: variable('value')
    ),
    format: sync(
        fn(int $result, string $label): string => "{$label}: {$result}",
        result: response('calculate'),
        label: variable('labelText')
    )
);

$result = run($workflow, value: 5, labelText: 'Result');
echo $result->response('format')->string();
// Output: Result: 15

Variables in conditional execution

use function Chevere\Workflow\{workflow, sync, variable, run};

$workflow = workflow(
    greet: sync(
        new Greet(),
        username: variable('username')
    )->withRunIf(
        variable('sayHello')
    )
);

$run = run(
    $workflow,
    username: 'Rodolfo',
    sayHello: true
);

Variables with Action classes

use Chevere\Demo\Actions\MyAction;
use function Chevere\Workflow\{workflow, sync, variable, run};

$workflow = workflow(
    greet: sync(
        MyAction::class,
        foo: variable('super')
    )
);

$result = run($workflow, super: 'Chevere');
echo $result->response('greet')->string();
// Output: Hello, Chevere

Notes

  • All declared variables must be provided when calling run()
  • Variable names are case-sensitive
  • Variables are type-checked based on the action’s parameter types
  • Variables can be used in job arguments and conditional expressions
  • Use response() to reference outputs from previous jobs instead of variables

Build docs developers (and LLMs) love