As expected the example below does not work but how can i tell php that my classname-string should be an uninstantiated class ? I know that i could work around with some tricks but is there a proper way?
thanks to magnus i like to add that in use I don't want to hard code the class name in the function it could be anything but taking care that it exists etc is not part of the question.
(for php>7.1)
The other direction works with get_class
.
Interface iFoo {
static function getBar(): string;
}
class Foo implements iFoo {
private static $bar = "`My test value`";
static function getBar(): string {
return self::$bar;
}
}
function getMyClass() : iFoo {
$classes = array('Foo');
print "this works: ".$classes[0]::getBar();
// return failes
print "this does not:";
return $classes[0];
}
Output:
this works: `My test value`this does not:
Fatal error: Uncaught TypeError: Return value of getMyClass() must implement interface iFoo, string returned
I know that it works with : iFoo | string
but then the typehinting is obsolete. I am missing something like a make-class-of-string() function.
Thank you for your help in advance :)