Constructing Results
Construction chooses the first branch and sets the initial metadata.
php
use Maxiviper117\ResultFlow\Result;
$payload = ['id' => 1];
$result = Result::ok($payload, ['request_id' => 'r-1']);What the constructors do
ok(...)creates a success branch.fail(...)creates a failure branch.failWithValue(...)stores the failed input in metadata underfailed_value.of(...)wraps a throwing callback and always wraps the callback return value as success.defer(...)normalizes callbacks that may return a value, aResult, or throw.
Why defer exists
of(...) is for callbacks that always return a plain value on success.
defer(...) is broader. Use it when the callback may already return a Result, because it preserves that result instead of wrapping it again.
That difference matters:
php
use Maxiviper117\ResultFlow\Result;
$of = Result::of(fn () => Result::fail('nope'));
$defer = Result::defer(fn () => Result::fail('nope'));
$of->isOk(); // true
$defer->isFail(); // trueof(...) treats the returned Result as plain data.
defer(...) treats the returned Result as the result to continue with.
What to remember
- Choose the branch explicitly when you already know it.
- Use
failWithValue(...)when the failed input matters. - Use
of(...)for plain-value callbacks that may throw. - Use
defer(...)for callbacks that may already returnResult. - Start metadata early if the flow will need correlation later.
Common mistakes
- Using
of(...)when the callback may return aResult. - Constructing a result without any metadata, then trying to recover context later.