0

I have the following code:

$id = $ui->getWidget('import_id')->value;

I know the class type of $ui. The getWidget() method can return one or more types of widget classes based on the parameter passed in. Some of those widget classes have a value parameter, and some don't.

If I want to add a doc hint to say "I know what this particular value of getWidget() is returning", can I do that without splitting up my code? i.e. I know I can accomplish this by adding an intermediate variable, e.g.:

/** @var KnownClassWithValueProperty $idClass */
$idClass = $ui->getWidget('import_id');
$id = $idClass->value;

But ideally I'd like to not do this, but somehow type-hint the value of $ui->getWidget('import_id').

Thanks!

2
  • 1
    To the best of my knowledge you can't type hint method chains like that without using the intermediate variable as you showed. This post from four years ago confirms that, and I couldn't find any updated guidance. One other option, which you probably don't want to do, either, is add a dedicated method to the class that $ui based on, with a getWidgetForBlahBlah(), and set the return value for that. Obviously this doesn't scale well, but you do get the added benefit of runtime type enforcement by PHP at least. Commented Jun 26, 2024 at 17:49
  • Thanks @ChrisHaas ... that's kinda what I expected. Oh well! Commented Jun 26, 2024 at 18:12

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.