samedi 29 septembre 2018

How to test if Callable parameter will return a string with Reflection?

I have a function which expects a Callable parameter. I want to determine that this callable returns a string and if it doesn't an exception should be thrown.

I tried searching for this, but no luck. Does the PHP reflection API provide functionality like this? I don't want to run the method and see if it actually returns a string.

Help would be appreciated.

Example of what I need:

class MyClass
{
    protected static $overrider = null;

    public static function setOverrider(Callable $callback)
    {
        // Pseudo code start
        if (!$callback returns string) {
            throw new \Exception('Wasnt a string!');
        }
        // Pseudo code end     

        self::$overrider = $callback;
    }
}





Aucun commentaire:

Enregistrer un commentaire