CKTIS / PHP
ReflectionClass
Preskúma triedu a vytvorí inštancie. newInstanceArgs spustí konštruktor s argumentmi a isInstantiable vopred odhalí rozhrania aj abstraktné triedy.
Verzia: PHP 8.4; repository runtime, production runtime not attested
Syntax
new ReflectionClass($classOrObject)Preskúma triedu a vytvorí inštancie. newInstanceArgs spustí konštruktor s argumentmi a isInstantiable vopred odhalí rozhrania aj abstraktné triedy. Súkromný konštruktor sa použitím metódy nestane verejným.
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]
Príklady
<?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()]);
Výsledok
[true,7,false]