3v4l.org

run code in 300+ PHP versions simultaneously
<?php trait T { public function self(): self { return $this; } public function parent(): parent { return $this; } public function static(): static { return $this; } } $selfType = (new ReflectionMethod(T::class, 'self'))->getReturnType(); var_dump($selfType::class, $selfType->getName()); $parentType = (new ReflectionMethod(T::class, 'parent'))->getReturnType(); var_dump($parentType::class, $parentType->getName()); $staticType = (new ReflectionMethod(T::class, 'static'))->getReturnType(); var_dump($staticType::class, $staticType->getName()); final class A { use T; } $selfType = (new ReflectionMethod(A::class, 'self'))->getReturnType(); var_dump($selfType::class, $selfType->getName()); $parentType = (new ReflectionMethod(A::class, 'parent'))->getReturnType(); var_dump($parentType::class, $parentType->getName()); $staticType = (new ReflectionMethod(A::class, 'static'))->getReturnType(); var_dump($staticType::class, $staticType->getName());
Output for 8.1.0 - 8.1.29, 8.2.0 - 8.2.23, 8.3.0 - 8.3.11
string(19) "ReflectionNamedType" string(4) "self" string(19) "ReflectionNamedType" string(6) "parent" string(19) "ReflectionNamedType" string(6) "static" string(19) "ReflectionNamedType" string(4) "self" string(19) "ReflectionNamedType" string(6) "parent" string(19) "ReflectionNamedType" string(6) "static"

preferences:
47.21 ms | 407 KiB | 5 Q