我没有得到这段代码的代码提示:
get_indexes
的 PHPDoc 我认为做得很好,NetBeans 似乎理解它并正确显示提示:
/**
* Get Index
*
* @global object $wpdb
* @param String $extension_table_name
* @return \ZRDN\Recipe[]
*/
public static function get_indexes($extension_table_name) {
global $wpdb;
$db_name = $wpdb->prefix . $extension_table_name;
$selectStatement = "SELECT * FROM `{$db_name}`";
$recipe_indexes = $wpdb->get_results($selectStatement);
return $recipe_indexes;
}
Recipe
定义在同一个命名空间下的同一个文件中:
class Recipe {
/**
* @var int
*/
public $recipe_id;
/**
* @var int
*/
public $post_id;
...
知道问题出在哪里吗?
最佳答案
如果您知道 $recipes
总是包含 Recipe
类型的对象,那么将它用作 map 的 $recipe
参数的类型功能:
$post_ids = array_map(function(Recipe $recipe) {
return $recipe->recipe_id;
}, $recipes);
这样 PhpStorm(和其他 IDE)可以帮助您进行自动完成,并且当 PHP 解释器在 $recipes
中遇到错误类型的值时会触发 fatal error 。
关于PhpStorm 没有在 array_map 中显示代码提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48040747/