CKTIS / PHP
ReflectionClass
Inspect a class and create instances with constructor arguments. newInstanceArgs runs the constructor with the supplied arguments, while isInstantiable lets tools reject interfaces or abstract classes first.
Version: PHP 8.4; repository runtime, production runtime not attested
Syntax
new ReflectionClass($classOrObject)Inspect a class and create instances with constructor arguments. newInstanceArgs runs the constructor with the supplied arguments, while isInstantiable lets tools reject interfaces or abstract classes first. Reflection does not make a private constructor publicly instantiable through this method.
class Meter {public function __construct(public int $n){}}
$ref=new ReflectionClass(Meter::class); $obj=$ref->newInstanceArgs([7]);
echo json_encode([$ref->isInstantiable(),$obj->n,(new ReflectionClass(Countable::class))->isInstantiable()]);
[true,7,false]
Examples
<?php
class Meter {public function __construct(public int $n){}}
$ref=new ReflectionClass(Meter::class); $obj=$ref->newInstanceArgs([7]);
echo json_encode([$ref->isInstantiable(),$obj->n,(new ReflectionClass(Countable::class))->isInstantiable()]);
Result
[true,7,false]