SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    930
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)

    PHPDoc type hint for variable that can be looped over or act as array?

    I'm not sure what exactly PHPDoc type hint should be for a variable that is either array or an object that can act like an array? I have come up with some ideas but I'm not 100% sure about them:

    The param has to be an array or an object that can be used in foreach:
    PHP Code:
        /**
         * @param array|Traversable $subscribers
         */ 
    The param has be be an array or an object that can use all ArrayAccess features like foreach, count(), square bracket element access, etc.:
    PHP Code:
        /**
         * @param array|ArrayAccess $subscribers
         */ 
    Am I doing it right?

  2. #2
    Non-Member
    Join Date
    Oct 2007
    Posts
    363
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure whether this is just a phpstorm thing or what, but I can tell you if you have an array of say $player objects, and you wanted to have code hinting work on the foreach loop, you can use a phpdoc syntax like this:
    PHP Code:
    /**
    *@var $players Player[]
    */

    foreach($players as $player){
     
    $player-> //the moment you do this in php storm, you'll get code hinting to all the public methods in a Player object

    Again, I'm not sure whether this is just a phpstorm thing, but I do it all the time because I find it very useful.

  3. #3
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    930
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    I use Netbeans and this works but with a slight modification:
    PHP Code:
    /* @var $players Player[] */ 
    I can also do this for parameter hints:
    PHP Code:
        /**
         * @param Subscriber[] $subscribers
         */ 
    This is nice, I didn't know about it! However, it only partially answers my question, because this case only covers a collection of objects. Can I describe collections of other data types in the same way? Like this:
    PHP Code:
        /**
         * @param string[] $subscribers
         * @param int[] $timestamps
         */ 
    ?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •