【问题标题】:var_dump() does not work on instances of gRPC Protobuf classesvar_dump() 不适用于 gRPC Protobuf 类的实例
【发布时间】:2019-03-14 05:33:04
【问题描述】:

这是我的 .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“消息”的类实例的属性被隐藏?

我尝试过类型转换:

$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

【问题讨论】:

    标签: php protocol-buffers grpc


    【解决方案1】:

    解决方法可能是 var_dump($request-&gt;serializeToJsonString())

    https://github.com/protocolbuffers/protobuf/blob/master/php/src/Google/Protobuf/Internal/Message.php#L1489

    【讨论】:

    • 这是我的问题的解决方案。但是,我仍然不明白是什么导致在调用var_dump($object) 时隐藏所有对象属性。
    • 对不起,我不知道。我猜它是 Message.php 中的东西。
    猜你喜欢
    • 2012-08-06
    • 2022-10-09
    • 2021-05-07
    • 2022-01-10
    • 2017-05-23
    • 1970-01-01
    • 1970-01-01
    • 2018-06-28
    • 2016-02-27
    相关资源
    最近更新 更多