1. Go to this page and download the library: Download denzyl/box library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
denzyl / box example snippets
/** @template T */
interface Item
{
/** @return T */
public function grab(): mixed;
}
/** @template TValue
* @param Item<TValue> $bag
* @return Result<TValue, Throwable>
*/
public static function put(Item $bag): Result
use Result\Item;
use Result\Box;
use Result\Result;
use App\Model\User;
use App\Exception\NotFoundException;
/**
* @implements Item<User>
*/
class UserFetcher implements Item
{
public function __construct(
private int $id,
private Database $db,
) {}
/** @return User */
public function grab(): mixed
{
$user = $this->db->findUser($this->id);
if ($user === null) {
throw new NotFoundException("User {$this->id} not found");
}
return $user;
}
}
/**
* @return Result<User, Throwable>
*/
function fetchUser(int $id): Result
{
return Box::put(new UserFetcher($id, $this->db));
}
// Direct — when you already have the value or error
$ok = Result::ok("Happy value"); // Result<string, never>
$err = Result::error(new Exception("Sad")); // Result<never, Exception>
// Try — wrap any callable that might throw
$res = Result::try(fn() => $this->riskyOp());
// When — conditional result without an if/else
$res = Result::when($age >= 18, "Access Granted", new Exception("Too young"));
$val = $res->collect(); // throws RuntimeException if Error
$val = $res->unwrapOr("default"); // returns "default" if Error
$val = $res->unwrapOrElse(fn($e) => ...); // calls closure with the error if Error
$val = $res->expect("User must exist"); // throws with your message if Error
// recover: turn error back into success with a computed value
$result->recover(fn(Exception $e) => "Fallback for: " . $e->getMessage());
// Result<never, Exception> → Result<string, never>
// orElse: turn error into a new Result (might still be an error)
$result->orElse(fn(Exception $e) => Result::error(new LoggedException($e)));
// Result<T, Exception> → Result<T, LoggedException>
// mapError: transform the error without changing Ok/Error state
$result->mapError(fn(Exception $e) => new DomainException("Wrapper", 0, $e));
// Result<T, Exception> → Result<T, DomainException>
// combine: fail-fast, get all values or first error
$combined = Result::combine([$r1, $r2, $r3]);
// any: first success, or last error if all failed
$any = Result::any([$r1, $r2, $r3]);
// partition: split into successes and failures
$partitioned = Result::partition([$r1, $r2, $r3]);