这是我的.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
型
2条答案
按热度按时间3phpmpom1#
解决方法可能是
var_dump($request->serializeToJsonString())
https://github.com/protocolbuffers/protobuf/blob/master/php/src/Google/Protobuf/Internal/Message.php#L1489
ldxq2e6h2#
您可以使用
symfony/var-dumper
和buggregator/trap
来转储protobuf消息。buggregator/trap
修补了Symfony的var-dumper(添加了protobuf消息渲染器),当您转储protobuf消息时,您将以紧凑和信息丰富的形式获得信息。字符串
sample of a dump in the terminal的