13

I'm working on a library which uses the phpDocumentor specification on determining the type system using reflection. However, I couldn't find any information about generic type declarations.

Is there any way a generic type declaration should be specified?

For example: is there any specification (in-progress) which specifies anything like:

/**
 * @template <T extends Base>
 */
class Collection {

    /**
     * @return Iterator<T>
     */
    function get_iterator();

}

Note that the code above is just an example to illustrate what I mean by generic type declarations. I do not want this achieve anything to do with collections and iterators.

11
  • PHP does not have generics? where are you getting this? Commented Sep 17, 2015 at 10:37
  • Doesn't this look a bit like you want abstract class Collection implements \Iterator {}? Commented Sep 17, 2015 at 10:40
  • 6
    @NDM I understand that it isn't supported in PHP. That doesn't mean it couldn't be supported in phpDocumentor or any derivation. That is what my question is about. Commented Sep 17, 2015 at 10:51
  • 4
    @NDM Because you don't understand the question, doesn't mean it isn't clear. Commented Sep 17, 2015 at 10:55
  • 2
    This is likely the next gen php doc, if it gets approved: github.com/phpDocumentor/fig-standards/blob/master/proposed/… Commented Jan 5, 2016 at 19:16

3 Answers 3

6

There apparently was some interest in the related topic of object specialization because that was added to PSR-5:

generic = collection-type "<" [type-expression "," *SP] type-expression ">"

Edit: and then removed as Jack notes below, albeit with a stated expectation that they'll return in one form or another before it's standardised

However, it doesn't provide a means to define a class as generic, and PSR-5 has been abandoned for a long while. The de-facto standard, phpDocumentor, does not in any way define support for such syntax.

If you want to informally mark up generics, you're best off making up your own syntax (inspired by similar syntax that already exists like Closure's documentation or JSDoc) while carefully avoiding anything that could actively confuse phpDocumentor, something like:

/**
 * @template {Base} T
 */
/**
 * @return Iterator {Iterator<T>}
 */

If you want to do it formally, you'll need to switch to another documentation system like doxygen.

Sign up to request clarification or add additional context in comments.

2 Comments

@JackWilsdon thanks, I've updated the answer to note this
2

You might want to check out the psalm project.

https://psalm.dev/docs/

https://github.com/vimeo/psalm

It supports generics in the documentation, as well as so much more. It's also used by Symfony's DB ORM, doctrine.

It supports Emacs, Phpstorm/Intellij, vim, and visual studio

Comments

0

Psalm supports this via

/**
 * @template T of Foo
 */

Ref: https://medium.com/vimeo-engineering-blog/uncovering-php-bugs-with-template-a4ca46eb9aeb

Comments

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.