<?php class Foo { function getKey() { return 1; } }; class Bar { function getId() { return 2; } }; class Repo { function getItems($id) { return ['got' => $id]; } } class Test { private Repo $repository; function __construct() { $this->repository = new Repo(); } function foo(Foo|Bar $obj): array { return $this->repository->getItems( match ($obj::class) { Foo::class => $obj->getKey(), Bar::class=> $obj->getId(), } ); } }; $obj = new Foo(); $test = new Test(); var_dump($test->foo($obj));
You have javascript disabled. You will not be able to edit any code.