如何将 PHP 对象转换为字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2537767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:53:34  来源:igfitidea点击:

How to convert a PHP object to a string?

php

提问by Rohan

Possible Duplicate:
PHP ToString() equivalent

可能的重复:
PHP ToString() 等效

I get this error:

我收到此错误:

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

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

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

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

请注意:我使用的代码适用于 PHP 4,但不适用于 PHP 5

Thanks!

谢谢!

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

编辑:我自己解决了。这很痛苦,但我做到了。无论如何,谢谢大家:-)

回答by Wim

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.

为什么需要这个字符串?如果您只是需要将其可视化以进行调试,您可以使用 var_dump()、print_r(),或者$s = print_r($var, 1);将其真正变成字符串以进行进一步的主题化。如果您需要将对象作为文本发送到其他地方(数据库、Javascript 代码),您可以选择序列化、json_encode、XML 转换等等,具体取决于您的具体情况。

回答by nickl-

You want to convert a PHP object to a string?

您想将 PHP 对象转换为字符串吗?

if neither var_dump, print_r, var_export, serialize, json_encodenor __toStringis exactly what you're after maybe this can help you satisfy your needs.

如果var_dumpprint_rvar_exportserializejson_encode__toString都不是您想要的,那么这可能可以帮助您满足您的需求。

For PHP 5.3 and above

对于 PHP 5.3 及以上

<?php
$v = (object) array('a' => 1, 'b' => 2, 'c' => 3);
$r = new ReflectionObject($v);

echo $r->getName() .' {' . implode(', ', array_map(
     function($p) use ($v) {
         $p->setAccessible(true);
         return $p->getName() .': '. $p->getValue($v);
     }, $r->getProperties())) .'}';

Will output:

将输出:

stdClass {a: 1, b: 2, c: 3}

For a more conventional approach compatible with previous PHP 5 flavours try

对于与以前的 PHP 5 风格兼容的更传统的方法,请尝试

<?php
class ExampleClass {
    private $pvt = 'private';
    protected $prot = 'protected';
    public $pub = 'public';
}

$v = new ExampleClass();
$r = new ReflectionObject($v);

echo $r->getName() ." {\n";
foreach ($r->getProperties() as $p)
   if ($p->isPublic())
       echo "\tpublic ".$p->getName().': '.$p->getValue($v)."\n";
   else
       echo "\t".($p->isPrivate()?'private ':'protected ').$p->getName().",\n";
echo "}\n";

Which will print:

这将打印:

ExampleClass {
    protected pvt,
    protected prot,
    public pub: public
}

回答by p4bl0

What you need is to add the magic method __toStringto your class so you can print what you want.

您需要的是将魔术方法添加__toString到您的类中,以便您可以打印您想要的内容。

Edit: Sorry, just saw "PHP 4, but not in PHP 5".

编辑:抱歉,刚刚看到“PHP 4,但不是 PHP 5”。

回答by Gabriel Solomon

you can use php magic method __toString

您可以使用 php 魔术方法__toString

回答by K. Norbert

I suspect that this is not a problem with the object not convertable to a string.

我怀疑这不是不可转换为字符串的对象的问题。

It's more like that you are assuming that a variable contains a string, (or something that can represented as a string), but it contains an object, and you are trying to echo that out.

这更像是假设一个变量包含一个字符串(或可以表示为字符串的东西),但它包含一个对象,而您正试图将其回显出来。

回答by back2dos

from the error you get I assume you used the object as a parameter to a function that expects a string. the question is, why you did that and what you expect that function to with the object. and then before passing it to the function, extract a sensible string value.

从您得到的错误中,我假设您将该对象用作需要字符串的函数的参数。问题是,你为什么这样做以及你期望该函数对对象有什么作用。然后在将其传递给函数之前,提取一个合理的字符串值。

回答by MAS1

You do not try to convert this Object in string,it will not work. Just fetch exact value from that Object.

您不要尝试将此对象转换为字符串,它不会起作用。只需从该对象中获取精确值。

As a example if you got output as cost.this cost output is Object. so fetch exact value from this as follows

举个例子,如果你的输出是成本。这个成本输出是对象。所以从这里获取确切的值如下

  $var=cost->Postage_cost;
  $var1=cost->other_cost;