Skip to content

Instantly share code, notes, and snippets.

@dbalabka
Last active July 20, 2026 13:34
Show Gist options
  • Select an option

  • Save dbalabka/651030146d59190357ab867527ed92f7 to your computer and use it in GitHub Desktop.

Select an option

Save dbalabka/651030146d59190357ab867527ed92f7 to your computer and use it in GitHub Desktop.
Fast and convinient strict typed DTO creation from REST plain array or stdClass response . Run https://3v4l.org/0uDPC#v8.1.19
<?php
declare(strict_types=1);
class SomeClass
{
public function __construct(
public readonly string $status
) {
}
public static function create(array|stdClass $params): self
{
$properties = get_class_vars(self::class);
return new self(...array_intersect_key((array) $params, $properties));
}
}
var_dump(SomeClass::create((object) ['status' => 'pending', 'foo' => 'bar']));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment