Dynamic Object Names – Navigating to method declarations in php storm

One of the things I love about PHPStorm is the way i can navigate to any function or method declaration just by cmd-clicking it (or ctrl-clicking on Windows), except when using dynamic object names.

However, there is a problem when you have a dynamically named object. Here I outline the problem, and also the solution, as provided by Maxim Makhankov of PHPStorm support.

the Problem

I sent the following support ticket to PHPStorm Support:
$id = 1; // (would come from a request, but here for example)
$object = 'Glossary';
$o = new $object($id);

A very simple example indeed, perfect to outline the issue – we cannot cmd-click the “Form” method here, as PHP Storm doesn’t know what it is. But…

The Solution

It was suggested that i use PhpDoc for type hinting, thus solving the issues introduced when using dynamic object names.

$id = 1; // (would come from a request, but here for example)
$object = 'Glossary';
* @var $o Glossary
$o = new $object($id);

Which does indeed work perfectly well. It also forces me to comment a little more…

1 Comment

Hello there, I think your website could possibly be having browser compatibility problems.
Whenever I take a look at your web site in Safari, it looks
fine however, when opening in Internet Explorer, it’s got some overlapping issues. I merely wanted to provide you with a quick heads up! Apart from that, excellent site!

Leave a Reply

Your email address will not be published. Required fields are marked *