json_decode-d 对象的 PHP 文档块 [英] PHP docblock for json_decode-d objects
本文介绍了json_decode-d 对象的 PHP 文档块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我从 PHP 代码中的 JSON 字符串获取对象.我希望我的 IDE (NetBeans) 知道对象的参数,而无需为它们创建特殊的类.
I'm getting objects from JSON string in my PHP code. I want my IDE (NetBeans) to know the parameters of the objects without creating special class for them.
我可以吗?
它看起来像:
$obj = json_decode($string);
/**
* @var $obj {
* @property int $id
* @property string $label
* }
*/
推荐答案
这是它的结构化版本
首先,您在 helpers
文件夹中的某处创建一个类
first, you make a class somewhere in your helpers
folder
<?php
namespace App\Helpers;
use function json_decode;
class JsonDecoder
{
public function loadString(string $string): self
{
$array = json_decode($string, true);
return $this->loadArray($array);
}
public function loadArray(array $array): self
{
foreach ($array as $key => $value) {
$this->{$key} = $value;
}
return $this;
}
}
那么,你要小心使用它
$obj= (new class() extends JsonDecoder {
public /** @var int */ $id;
public /** @var string */ $label;
});
$obj->loadString($string);
echo $obj->id;
echo $obj->label;
这篇关于json_decode-d 对象的 PHP 文档块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文