7

PHPDoc provides the @var tag, which should work even for variables declared outside of a class.

However, this does not seem to work if I define the variable as a magic member of an object:

/** @var $app->translator \Fortress\MessageTranslator */
$app->translator = new \Fortress\MessageTranslator();

Where $app is a Slim object that supports arbitrary property assignment via magic setters and getters.

I know that I could add it to Slim itself via the @property tag, but then I would need to change the core Slim code every time I create a new property.

Does PHPDoc support this kind of dynamic property documenting?

alexw
  • 8,468
  • 6
  • 54
  • 86
  • 1
    Did you try removing `$app->translator` in the doc section? Anyway [this could help you](http://stackoverflow.com/questions/6395737/how-do-i-make-my-php-ide-understand-dependency-injection-containers). – Davide Pastore Jun 23 '15 at 13:55
  • Did you ever solve this? – Andy Jul 30 '16 at 12:22
  • Andy, I've started using Slim 3, which uses an explicit DI container. So, something like what is shown in @onerror's answer _should_ work. A similar answer is given in the link above. – alexw Jul 30 '16 at 13:52
  • @alexw Thanks, I was only trying to get type hinting working in my IDE for a dynamically assigned property. Ended up using a local variable and type hinting that with `@var` instad. – Andy Jul 30 '16 at 14:26
  • And this is just one example of why magic is bad. – Jimbo Jul 04 '17 at 09:36

1 Answers1

-1

You don't need $app->translator in the doc block. It should look like this:

/** @var \Fortress\MessageTranslator your_possible_comments */

or

/** @type \Fortress\MessageTranslator your_possible_comments */

Link to the documentation.

onerror
  • 606
  • 5
  • 20