【发布时间】: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