PHP7将类型返回为JSON [英] PHP7 return type to JSON
问题描述
PHP 7具有一个新功能,即返回类型声明.
PHP 7 has a new feature which is a return type declaration.
我们可以返回一个字符串",例如:
We can return type a 'string' like:
function myFunction ($a) : string { }
我们还可以返回一个'array'的类型,例如:
We can also return type an 'array' like:
function myFunction ($a) : array { }
但是我们如何声明'JSON'类型的响应?
But how can we declare a 'JSON' type of response?
推荐答案
JSON不是PHP中的本机数据类型,而是结构化字符串.因此,如果您的函数返回JSON,则您将返回一个字符串.
JSON isn't a native datatype in PHP, it's a structured string. So if your function returns JSON, you're returning a string.
所以function myFunction ($a) : string { }
是正确的.
如果要进一步描述退货,则应使用文档.
If you want to describe the return further you should be using docs.
/**
* @return string $jsonString The returned string contains JSON
*/
function myFunction ($a) : string { }
PHP中的序列化对象也是如此.序列化的对象是结构化的字符串.
The same also goes for serialized objects in PHP. A serialized object is a structured string.
这篇关于PHP7将类型返回为JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!