如何将PHP对象转换为字符串? [英] How to convert a PHP object to a string?

查看:396
本文介绍了如何将PHP对象转换为字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
等同于PHP ToString()

Possible Duplicate:
PHP ToString() equivalent

我收到此错误:

Catchable fatal error: Object of class stdClass could not be converted to string

所以,我的问题是,如何在PHP中将对象转换为字符串?我不想序列化它.

So, my question is, how do I convert an object to a string in PHP? I don't want to serialize it though.

请注意:我使用的代码在PHP 4中有效,但在PHP 5中无效

Just a note: the code I use works in PHP 4, but not in PHP 5

谢谢!

我自己解决了这个问题.这很痛苦,但我做到了.无论如何,谢谢大家:-)

I resolved it myself. It was a pain, but I did it. Thanks anyway, everyone :-)

推荐答案

为什么需要此字符串?如果只需要对其进行可视化以进行调试,则可以使用var_dump(),print_r()或$s = print_r($var, 1);使其真正成为字符串以进行进一步的主题化.如果需要将对象作为文本发送到其他地方(数据库,Javascript代码),则可以根据实际情况选择序列化,json_encode,XML转换等等.

Why do you need this string? If you just need to visualize it for debugging, you can use var_dump(), print_r(), or $s = print_r($var, 1); to really make it into a string for further theming. If you need to send the object as text to somewhere else (database, Javascript code), you have a choice of serialize, json_encode, XML conversion, and many more, depending on your exact situation.

这篇关于如何将PHP对象转换为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆