Good day.

Tell me, is it possible with PhpDoc to comment on methods that return dynamically generated objects. You need this to call for hints in IDE PhpStrom.

The construction of the @return first|second not very convenient, since the number of objects generated is not limited.

Primitive example:

 class first { function getMain() { return 'Hello'; } } class second { function getSecond() { return "SeconD"; } } /** * @param $name * */ function getClass( $name ) { return new $name; } $class = getClass( 'first' ); 

Thanks for the help!

    2 answers 2

    To get hints in phpStorm , you need to use the .phpstorm.meta.php file:

     <?php namespace PHPSTORM_META { // we want to avoid the pollution /** @noinspection PhpUnusedLocalVariableInspection */ /** @noinspection PhpIllegalArrayKeyTypeInspection */ $STATIC_METHOD_TYPES = [ \ORM::factory('') => [ 'File' instanceof \Model_File, 'Gallery' instanceof \Model_Gallery, 'User' instanceof \Model_User, // ... ], ]; } 

    This example indicates which class object will be handled after calls, like:

     ORM::factory('User')->... 

    The file must be added to the project root and restarted IDE.
    How to sharpen specifically for your example with the function, I will not say, but you can see the documentation on this file somewhere here: PhpStorm Advanced Metadata .

    • one
      Thank you very much! Wow, how cool it is :) With the function, it looks like $ STATIC_METHOD_TYPES = [\ myFunction ('') => [........ - zsiteru
    • And thank you for the addition. - xEdelweiss

    But why not just use the @var comment?

     /** $var $class first */ $class = getClass( 'first' ); 

    Supplement after it works great.

    • Each time you call getClass ('first'); writing / ** $ var $ class first * / is tiring, besides @xEdelweiss offered a great solution. - zsiteru