php var_dump()不适用于gRPC Protobuf类的示例

zqry0prt  于 11个月前  发布在  PHP
关注(0)|答案(2)|浏览(105)

这是我的.proto文件:

message HelloReply {
  string message = 1;
}

字符串
下面是使用grpc_php_plugin运行protoc的输出php:

<?php
# Generated by the protocol buffer compiler.  DO NOT EDIT!
# source: helloworld.proto

namespace Helloworld;

use Google\Protobuf\Internal\GPBType;
use Google\Protobuf\Internal\RepeatedField;
use Google\Protobuf\Internal\GPBUtil;

    /**
 * The request message containing the user's name.
 *
 * Generated from protobuf message <code>helloworld.HelloRequest</code>
 */
class HelloRequest extends \Google\Protobuf\Internal\Message
{
    /**
     * Generated from protobuf field <code>string name = 1;</code>
     */

    private $name = '';

    /**
     * Constructor.
     *
     * @param array $data {
     *     Optional. Data for populating the Message object.
     *
     *     @type string $name
     * }
     */
    public function __construct($data = NULL) {
        \GPBMetadata\Helloworld::initOnce();
        parent::__construct($data);
    }

    /**
     * Generated from protobuf field <code>string name = 1;</code>
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Generated from protobuf field <code>string name = 1;</code>
     * @param string $var
     * @return $this
     */
    public function setName($var)
    {
        GPBUtil::checkString($var, True);
        $this->name = $var;

        return $this;
    }

}


下面是我的代码中的一段代码:

$name="test_name";
$request = new Helloworld\HelloRequest(array("name"=>$name));
echo "Name is " . $request->getName() . "\n";
var_dump($request);


输出如下:

Name is test_name
object(Helloworld\HelloRequest)#3 (0) {
}


为什么var_dump没有给给予我存储在“HelloRequest”对象中的“name”的值?我如何输出gRPC生成的类的示例的所有属性及其值?是什么导致扩展gRPC“Message”的类的示例的属性被隐藏?
我试过类型转换:

$strvar = (string) $request;


导致以下错误:

PHP Recoverable fatal error:  Object of class Helloworld\HelloRequest could not be converted to string in /home/........./php/greeter_client.php on line 42

3phpmpom

3phpmpom1#

解决方法可能是var_dump($request->serializeToJsonString())
https://github.com/protocolbuffers/protobuf/blob/master/php/src/Google/Protobuf/Internal/Message.php#L1489

ldxq2e6h

ldxq2e6h2#

您可以使用symfony/var-dumperbuggregator/trap来转储protobuf消息。
buggregator/trap修补了Symfony的var-dumper(添加了protobuf消息渲染器),当您转储protobuf消息时,您将以紧凑和信息丰富的形式获得信息。

\dump($message);

字符串
sample of a dump in the terminal

相关问题