在 Symfony 中输入提示 json 对象 [英] Type hinting json object in Symfony
问题描述
在我的 Symfony 2.3 项目中,我有一个 curl 函数,它从 API 接收 JSON 数据并将此数据作为参数传递给另一个函数.目前,接受 JSON 对象的函数如下所示:
In my Symfony 2.3 project, I have a curl function that receives JSON data from an API and passes this data as a parameter to another function. Currently, the function that accepts the JSON object looks like this:
public function updateCountries($data)
{
foreach (json_decode($data, true) as $key => $value) {
(do something)
}
}
我想向 $data 参数添加一个类型提示,以表明这是一个 JSON 对象,但JSON"类型似乎不存在.有没有有效的类型提示?
I'd like to add a type hint to the $data parameter to indicate that this is a JSON object, but a "JSON" type doesn't seem to exist. Is there a type hint that would work?
推荐答案
PHP 不支持 JSON 作为类型.$data 将作为字符串出现并在您对其调用 json_decode() 时转换为数组.您可以创建一个将 json 字符串作为参数并返回一个数组的方法.然后使用数组输入 hit updateCountries.
PHP doesn't support JSON as a type. $data would come in as a string and be converted to a array when you call json_decode() on it. You can make a method that takes a json string as a argument and returns a array. Then type hit updateCountries with Array.
这篇关于在 Symfony 中输入提示 json 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!