I do not know how to put it exactly. There is an array variable, you need autocomplete in phpstorm or type hinting or highlighting the array keys.

For example, if I describe a variable with a class, then all the methods and properties will be highlighted:

/** * @var \PDO */ 

I tried to search and try, but nothing came up, suggestive (if I incorrectly express myself) examples in the burzhunete:

https://gist.github.com/mvriel/3822861
https://stackoverflow.com/questions/2713710/comment-associative-array-in-php-documentor https://stackoverflow.com/questions/15414103/best-way-to-document-array-options-in-phpdoc

Is it possible? An example of an array to describe:

 $array = [ 45 => [ 'curl_options' => [ CURLOPT_URL => 'http:', CURLOPT_RETURNTRANSFER => true, CURLOPT_POST => true, ], 'curl_postfield' => 'id', 'status_pattern' => '/[\da-z]{13}\s*(.+)/isu' ], 46 => [ 'curl_options' => [ CURLOPT_URL => 'http:', CURLOPT_RETURNTRANSFER => true, CURLOPT_POST => true, ], 'curl_postfield' => 'post', 'status_pattern' => '/[\da-z]{10}\s*(.+)/isu' ] ]; 
  • If all elements of the array are of the same type, then yes - it is enough to specify @type \PDO[] . If the elements of the array are of different types, then I don’t know how to set the key-type correspondence, but this construction itself is a code smell, and it’s worth getting rid of. - etki
  • @Etki updated the question with an example array. - Jean-Claude

1 answer 1

If you have a regular array of objects of the same type, then it is simple:

 $array = []; /** @var $array ExampleClass[] */ foreach ($array as $object) { // $object->getS... сработает автодополнение $object->getSomething(); } 

If you have a complex array, you cannot do without switching to nested objects.

 class ExampleClass { /** @var ExampleEmbeddedClass[] */ public $list = []; public function getSomething() {} } class ExampleEmbeddedClass { public $property = 1; } 

In this case, autocompletion works great:

autocompletion

Conversion to an array does not represent a problem, although, in the simplest form, it is done suboptimally through conversion into JSON and back:

 $array = [ 42 => new ExampleClass(), ]; /** @var $array ExampleClass[] */ $array[42]->list[] = new ExampleEmbeddedClass(); $cleanArray = json_decode(json_encode($array), true); var_export($cleanArray); 

It turns out such a simple array:

 array ( 42 => array ( 'list' => array ( 0 => array ( 'property' => 1, ), ), ), ) 

If you know exactly what keys you will have in the array, then it may be better to do the conversion explicitly. Or else to abandon this idea altogether, at least with regard to the properties of cURL.

  • did not understand anything. - Jean-Claude
  • associative arrays do not work like this - use objects - sanmai
  • if you use an object instead of an array to describe a config, will auto-code completion work? do you mean it? - Jean-Claude
  • yes, it will work - sanmai