Skip to content
Advertisement

How to infer type of a dynamically instantiated class without using annotations?

I have a dynamic config where I’ve defined supported classes for usage:

JavaScript

All of those classes extend an abstract class AbstractParameter.

Now when I am registering those in the package I’m developing, I am doing this:

JavaScript

And this is working fine, however I need to say that $instance is actually an AbstractParameter because otherwise my IDE won’t pick up that the method is from that class.

Is there a way to do this without annotations?

Advertisement

Answer

There is absolutely nothing wrong with using type annotations. That’s what they are there for.

But I agree that proper type hinting is better and more robust.

But for that you cannot use not use dynamic instantiation. Use an abstract factory instead. This will make the code clearer, more flexible, and more explicit.

JavaScript

Then you would do:

JavaScript

As an aside, I would create a ParameterInterface and type-hint for that instead of an abstract class.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement