basho / php-protobuf

PHP Protobuf - Fast PHP Protocol Buffers implementation
Apache License 2.0
5 stars 8 forks source link

The generate code don't use \ProtobuffMessage class #4

Closed boboxiaodd closed 7 years ago

boboxiaodd commented 7 years ago

@christophermancini I use this command line:

php protoc-gen-php.php -I. -o./tests/chat -D chat chat.proto
# protoc --version
libprotoc 3.2.0
# php -v
PHP 7.0.17 (cli) (built: Mar 22 2017 12:01:33) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
# php --re protobuf
Extension [ <persistent> extension #29 protobuf version 0.11.0 ] {

  - Classes [1] {
    Class [ <internal:protobuf> abstract class ProtobufMessage ] {

      - Constants [8] {
        Constant [ integer PB_TYPE_DOUBLE ] { 1 }
        Constant [ integer PB_TYPE_FIXED32 ] { 2 }
        Constant [ integer PB_TYPE_FIXED64 ] { 3 }
        Constant [ integer PB_TYPE_FLOAT ] { 4 }
        Constant [ integer PB_TYPE_INT ] { 5 }
        Constant [ integer PB_TYPE_SIGNED_INT ] { 6 }
        Constant [ integer PB_TYPE_STRING ] { 7 }
        Constant [ integer PB_TYPE_BOOL ] { 8 }
      }

One of php source is:

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

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

/**
 * Protobuf type <code>Login_sc</code>
 */
class Login_sc extends \Google\Protobuf\Internal\Message
{
    /**
     * <code>.Login_sc.LoginReturn return = 1;</code>
     */
    private $return = 0;

    public function __construct() {
        \GPBMetadata\Chat::initOnce();
        parent::__construct();
    }

    /**
     * <code>.Login_sc.LoginReturn return = 1;</code>
     */
    public function getReturn()
    {
        return $this->return;
    }

    /**
     * <code>.Login_sc.LoginReturn return = 1;</code>
     */
    public function setReturn($var)
    {
        GPBUtil::checkEnum($var, \\Login_sc_LoginReturn::class);
        $this->return = $var;
    }

}

But i find the test code is extends \ProtobufMessage

<?php
/**
 * Auto generated from test.proto at 2016-08-31 07:38:24
 */

namespace {
/**
 * Bar message
 */
class Bar extends \ProtobufMessage
{
    /* Field index constants */
    const DOUBLE_FIELD = 1;

    /* @var array Field descriptors */
    protected static $fields = array(
        self::DOUBLE_FIELD => array(
            'name' => 'double_field',
            'required' => true,
            'type' => \ProtobufMessage::PB_TYPE_DOUBLE,
        ),
    );

    /**
     * Constructs new message container and clears its internal state
     */
    public function __construct()
    {
        $this->reset();
    }
christophermancini commented 7 years ago

@boboxiaodd Try using the v2-alpha release. It appears you might be using the branch / code that is merged together from the work the previous maintainer was working on.

christophermancini commented 7 years ago

I am going to do some cleanup work on the repository so this is going to be less confusing going forward.

boboxiaodd commented 7 years ago

Thank you @christophermancini , It work, but maybe it don't suppourt proto3 syntax like this:

syntax = "proto3";
message Login_cs {
    uint32 uid = 1;
    string deviceid = 2;
    uint64 timestamp = 3;
    string authstr = 4;
}

it's nothing , i can change the syntax.

christophermancini commented 7 years ago

@boboxiaodd Ah, ok. Yea, proto3 support is not there. However if you are using proto3, you can use the new official Google PHP library. For Riak, we are using proto2 and can't upgrade to 3 without a great deal of work across the org, so I have to stick with 2 for now.