Skip to content

Instantly share code, notes, and snippets.

@paq85
Created December 10, 2013 16:01
Show Gist options
  • Save paq85/7893044 to your computer and use it in GitHub Desktop.
Save paq85/7893044 to your computer and use it in GitHub Desktop.
PHPDoc of Array Key ?
class Foo
{
/**
* @type array [
* @key string Bar ID (MD5)
* @value Bar $bar
* ]
*
* OR
*
* @type array (
* @type string $key Bar ID (MD5) => @type Bar $value
* )
*
* ???
*/
private $bars;
public function addBar(Bar $bar)
{
$this->bars[$bar->getId()] = $bar;
}
public function findBarById($barId)
{
/**
* Thanks to the fact I know $this->bars Key is Bar::id I don't have to iterate through the whole array
* and check if ($barId === $bar->getId())
*/
if (isset($this->bars[$barId])) {
return $this->bars[$barId];
}
return false;
}
}
@gnugat
Copy link

gnugat commented Dec 11, 2013

What about:

<?php

class Foo
{
    /**
     * @var array of Bar indexed by its ID
     */
    private $bars;
}

@jfsimon
Copy link

jfsimon commented Dec 11, 2013

Why not:

<?php

class Foo
{
    /**
     * @var Bar[string]
     */
    private $bars;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment