This question already has an answer here:
这个问题在这里已有答案:
- PHP json_encode class private members 8 answers
- PHP json_encode类私有成员8个答案
Consider this stripdown class:
考虑这个条带类:
class foo {
private $fooVar;
public getFooVar(){
}
}
$fooObj = new foo();
want i want/need to do is convert this to JSON format so I thought of putting
我希望/需要做的是将其转换为JSON格式,所以我想放
echo json_encode($fooObj);
however it doesnt work, i don't have fooVar and im guessing because $fooVar is private..
但它不起作用,我没有fooVar和我猜测因为$ fooVar是私人..
any other way I can do this without having to compromise and make $fooVar public?
我可以做任何其他方式,而不必妥协并公开$ fooVar?
Thanks a bunch!
谢谢你!
3 个解决方案
#1
3
If you're using PHP 5.3 (or earlier), you can't really do what you're asking for easily. You won't get the methods encoded in JSON because JSON is a data format and doesn't include functions, and you won't get the property because it's a private property. You know this much already, but it's worth re-iterating to point out that you're right, it's not easy. Even writing the getter as a magic __get()
method to turn it into a read-only public property won't work in this case.
如果您使用的是PHP 5.3(或更早版本),则无法轻松完成您的要求。您将不会获得以JSON编码的方法,因为JSON是一种数据格式,并且不包含函数,并且您不会获取该属性,因为它是私有属性。你已经知道了这一点,但值得重新指出你是对的,这并不容易。即使将getter作为一个神奇的__get()方法将其转换为只读公共属性,也不会在这种情况下起作用。
In PHP 5.3, there are a couple of work arounds you could use.
在PHP 5.3中,您可以使用几种解决方法。
-
Reflection
. PHP includes a Reflection API, which allows you to access details about objects and classes, including reading values from private members. Obviously, however, this would mean writing a fair bit of code to get at the value, and it won't be fast either.反射。 PHP包含一个Reflection API,它允许您访问有关对象和类的详细信息,包括从私有成员读取值。然而,显然,这意味着要编写一些代码以获得该值,并且它也不会很快。
-
Write the
json_encode
call inside the class. That way you can include private properties. I would suggest usingget_object_vars($this)
inside the object to get at the private properties in a JSON friendly way.在类中写入json_encode调用。这样你就可以包括私人财产。我建议在对象中使用get_object_vars($ this)以JSON友好的方式获取私有属性。
If you're using PHP 5.4, you have an additional option:
如果您使用的是PHP 5.4,则还有一个选项:
- PHP 5.4 introduces a
JsonSerializable
interface. Addimplements JsonSerializable
to your class, and write a method calledjsonSerialize()
. This method should return the data that you want to be in the JSON string. You can include or exclude whichever public or private properties you want. - PHP 5.4引入了JsonSerializable接口。将实现JsonSerializable添加到您的类,并编写一个名为jsonSerialize()的方法。此方法应返回您要在JSON字符串中的数据。您可以包含或排除您想要的任何公共或私人财产。
PHP 5.5 hasn't been released yet, but it will add another option for you:
PHP 5.5尚未发布,但它将为您添加另一个选项:
-
PHP 5.5 will introduce a more complex syntax for defining properties, which will allow you to define properties as public but read-only, or to include more complex logic for them in the getter and setter, but still have them as properties. It will work much like the__get()
and__set()
methods do now, but with the variables explicitly defined as properties, which will mean that they should be accessible to the JSON serialiser, whereas currently with__get()
they're not.
[EDIT] - note, the above paragraph is incorrect; the feature described did not make it into the PHP 5.5 release. Too early yet to say yet whether it'll get into 5.6 or not. - PHP 5.5将引入更复杂的语法来定义属性,这将允许您将属性定义为公共但只读,或者在getter和setter中包含更复杂的逻辑,但仍将它们作为属性。它的工作方式与现在的__get()和__set()方法非常相似,但是变量显式定义为属性,这意味着它们应该可以被JSON序列化程序访问,而目前使用__get()则不行。 [编辑] - 注意,上段不正确;所描述的功能没有进入PHP 5.5版本。太早还没有说它是否会进入5.6。
Hope that helps.
希望有所帮助。
#2
1
JSON decoding is a bit tricky with custom types, since you do not have information about type in JSON. All you have is generic objects (hash tables) and arrays.
JSON解码对于自定义类型有点棘手,因为您没有关于JSON类型的信息。你所拥有的只是通用对象(哈希表)和数组。
But to serialize objects to JSON, you can implement JsonSerializable
interface. Then json_encode
function will do what you want.
但是要将对象序列化为JSON,您可以实现JsonSerializable接口。然后json_encode函数将执行您想要的操作。
#3
0
This would print a JSON with all of the properties (public, private and protected) of class foo
:
这将打印一个包含类foo的所有属性(public,private和protected)的JSON:
$reflection = new ReflectionClass('foo');
$properties = $reflection->getdefaultProperties();
echo json_encode($properties);
#1
3
If you're using PHP 5.3 (or earlier), you can't really do what you're asking for easily. You won't get the methods encoded in JSON because JSON is a data format and doesn't include functions, and you won't get the property because it's a private property. You know this much already, but it's worth re-iterating to point out that you're right, it's not easy. Even writing the getter as a magic __get()
method to turn it into a read-only public property won't work in this case.
如果您使用的是PHP 5.3(或更早版本),则无法轻松完成您的要求。您将不会获得以JSON编码的方法,因为JSON是一种数据格式,并且不包含函数,并且您不会获取该属性,因为它是私有属性。你已经知道了这一点,但值得重新指出你是对的,这并不容易。即使将getter作为一个神奇的__get()方法将其转换为只读公共属性,也不会在这种情况下起作用。
In PHP 5.3, there are a couple of work arounds you could use.
在PHP 5.3中,您可以使用几种解决方法。
-
Reflection
. PHP includes a Reflection API, which allows you to access details about objects and classes, including reading values from private members. Obviously, however, this would mean writing a fair bit of code to get at the value, and it won't be fast either.反射。 PHP包含一个Reflection API,它允许您访问有关对象和类的详细信息,包括从私有成员读取值。然而,显然,这意味着要编写一些代码以获得该值,并且它也不会很快。
-
Write the
json_encode
call inside the class. That way you can include private properties. I would suggest usingget_object_vars($this)
inside the object to get at the private properties in a JSON friendly way.在类中写入json_encode调用。这样你就可以包括私人财产。我建议在对象中使用get_object_vars($ this)以JSON友好的方式获取私有属性。
If you're using PHP 5.4, you have an additional option:
如果您使用的是PHP 5.4,则还有一个选项:
- PHP 5.4 introduces a
JsonSerializable
interface. Addimplements JsonSerializable
to your class, and write a method calledjsonSerialize()
. This method should return the data that you want to be in the JSON string. You can include or exclude whichever public or private properties you want. - PHP 5.4引入了JsonSerializable接口。将实现JsonSerializable添加到您的类,并编写一个名为jsonSerialize()的方法。此方法应返回您要在JSON字符串中的数据。您可以包含或排除您想要的任何公共或私人财产。
PHP 5.5 hasn't been released yet, but it will add another option for you:
PHP 5.5尚未发布,但它将为您添加另一个选项:
-
PHP 5.5 will introduce a more complex syntax for defining properties, which will allow you to define properties as public but read-only, or to include more complex logic for them in the getter and setter, but still have them as properties. It will work much like the__get()
and__set()
methods do now, but with the variables explicitly defined as properties, which will mean that they should be accessible to the JSON serialiser, whereas currently with__get()
they're not.
[EDIT] - note, the above paragraph is incorrect; the feature described did not make it into the PHP 5.5 release. Too early yet to say yet whether it'll get into 5.6 or not. - PHP 5.5将引入更复杂的语法来定义属性,这将允许您将属性定义为公共但只读,或者在getter和setter中包含更复杂的逻辑,但仍将它们作为属性。它的工作方式与现在的__get()和__set()方法非常相似,但是变量显式定义为属性,这意味着它们应该可以被JSON序列化程序访问,而目前使用__get()则不行。 [编辑] - 注意,上段不正确;所描述的功能没有进入PHP 5.5版本。太早还没有说它是否会进入5.6。
Hope that helps.
希望有所帮助。
#2
1
JSON decoding is a bit tricky with custom types, since you do not have information about type in JSON. All you have is generic objects (hash tables) and arrays.
JSON解码对于自定义类型有点棘手,因为您没有关于JSON类型的信息。你所拥有的只是通用对象(哈希表)和数组。
But to serialize objects to JSON, you can implement JsonSerializable
interface. Then json_encode
function will do what you want.
但是要将对象序列化为JSON,您可以实现JsonSerializable接口。然后json_encode函数将执行您想要的操作。
#3
0
This would print a JSON with all of the properties (public, private and protected) of class foo
:
这将打印一个包含类foo的所有属性(public,private和protected)的JSON:
$reflection = new ReflectionClass('foo');
$properties = $reflection->getdefaultProperties();
echo json_encode($properties);