How do I get a template class name inside an extending class method? #10133
designermonkey
started this conversation in
General
Replies: 1 comment 3 replies
-
There's no runtime support for generics in PHP, with or without Psalm. While you probably can parse the docblock and figure out the generic parameter, that sounds like a lot of work that could be avoided. You could just let the extending class provide the type for you, e.g.: /**
* @template TConfiguration of Configuration
*/
abstract class Application
{
/**
* @psalm-return class-string<TConfiguration>
*/
abstract public function getConfigurationClass(): string;
}
// somewhere in the client code
class CustomerPortalConfiguration extends Configuration {}
/** @extends Application<CustomerPortalConfiguration> */
class CustomerPortalApplication extends Application
{
public function getConfiguration(): string
{
return CustomerPortalConfiguration::class;
}
} |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Is there any way, or with any utilities, to get a class name used as a generic template value inside my method code?
For example, in this Java code:
You can see that it's trivial to get the name of the class
T
extendingConfiguration
as generics are a language feature.So far, I know that in PHP:
My question is, what should I be doing in the
getConfigurationClass
method? Are there any utilities in Psalm that allow me to do this, or do I have to doReflectionClass
work to get the doc strings and regex parse them?Beta Was this translation helpful? Give feedback.
All reactions