1. Go to this page and download the library: Download tommyknocker/struct 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/ */
tommyknocker / struct example snippets
use tommyknocker\struct\CollectionType;
use tommyknocker\struct\Field;
use tommyknocker\struct\Struct;
use tommyknocker\struct\validation\rules\CountRule;
use tommyknocker\struct\validation\rules\RangeRule;
enum OrderStatus: string
{
case Paid = 'paid';
}
final class OrderItem extends Struct
{
#[Field]
public readonly string $sku;
#[Field(validationRules: [new RangeRule(1, 100)])]
public readonly int $quantity;
}
final class Order extends Struct
{
#[Field(alias: 'order_id', outputAlias: 'id')]
public readonly string $orderId;
#[Field]
public readonly OrderStatus $status;
#[Field]
public readonly DateTimeImmutable $createdAt;
#[Field(OrderItem::class, collection: CollectionType::List, validationRules: [new CountRule(min: 1)])]
public readonly array $items;
}
$order = Order::fromJson('{
"order_id": "ord-42",
"status": "paid",
"createdAt": "2026-10-25T12:00:00+03:00",
"items": [{"sku": "A", "quantity": 2}]
}');
$order->items[0]->quantity; // int: 2
$order->status; // OrderStatus::Paid
$payload = $order->toArray();
// [
// 'id' => 'ord-42',
// 'status' => 'paid',
// 'createdAt' => '2026-10-25T12:00:00.000000+03:00',
// 'items' => [['sku' => 'A', 'quantity' => 2]],
// ]
$json = $order->toJson(pretty: true);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.