mercredi 21 novembre 2018

How can I find what class type is expected for a method's property/properties?

I need to see what class type is being expected (type-hinted) for a method property.

<?php

class Foo {}

class Bar {
    public function do(Foo $foo_instance) {}
}

$bar = new Bar();
$some_instance = new ??();
$bar->do($some_instance);

?>

I think this is something available in the Reflection API, but I have yet to find anything that spits out 'Foo' as my type-hint for Bar::do. Any ideas?

Context

I want to do something like:

<?php
...
if ( myMethodExpects($class, $method, 'Foo') ) {
    $some_instance = new Foo();
} elseif ( myMethodExpects($class, $method, 'Baz') {
    $some_instance = new Baz();
} elseif ( myMethodHasNoTypeHint($class, $method) ) {
    $some_instance = 'just a string';
}
...
?>





Aucun commentaire:

Enregistrer un commentaire