allegro / php-protobuf

PHP Protobuf - Google's Protocol Buffers for PHP

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

PHP Notice when trying to compile a proto file and failure when creating output PHP file

bieli opened this issue · comments

I get the following "PHP Notice" when trying to compile a proto file ( from Google tutorial https://developers.google.com/protocol-buffers/docs/pythontutorial). I can't see output PHP file from parser after parsing addressbook.proto.

Below I publish my steps and error message from PHP:

user@bieli:php-protobuf/example$ cat > addressbook.proto
package tutorial;

message Person {
  required string name = 1;
  required int32 id = 2;
  optional string email = 3;

  enum PhoneType {
    MOBILE = 0;
    HOME = 1;
    WORK = 2;
  }

  message PhoneNumber {
    required string number = 1;
    optional PhoneType type = 2 [default = HOME];
  }

  repeated PhoneNumber phone = 4;
}

message AddressBook {
  repeated Person person = 1;
}

^Z
[2]+  Stopped                 cat > addressbook.proto
user@bieli:php-protobuf/example$ php ../protoc-php.php addressbook.proto 
PHP Notice:  Undefined index: PhoneType in php-protobuf/ProtobufCompiler/ProtobufParser.php on line 1039
Type PhoneType not defined

My general problem it's failure output PHP file.

For comparision, when I using Google tools for Python ( https://developers.google.com/protocol-buffers/docs/pythontutorial ) I can see output file, after default 'protoc' command:

user@bieli:php-protobuf$ protoc --proto_path=./ --python_out=/tmp/ example/addressbook.proto

user@bieli:php-protobuf$ file /tmp/example/addressbook_pb2.py 
/tmp/example/addressbook_pb2.py: Python script, ASCII text executable, with very long lines

In Python code I can see important section:

_PERSON_PHONETYPE = descriptor.EnumDescriptor(
  name='PhoneType',
  full_name='tutorial.Person.PhoneType',
  filename=None,
  file=DESCRIPTOR,
  values=[
    descriptor.EnumValueDescriptor(
      name='MOBILE', index=0, number=0,
      options=None,
      type=None),
    descriptor.EnumValueDescriptor(
      name='HOME', index=1, number=1,
      options=None,
      type=None),
    descriptor.EnumValueDescriptor(
      name='WORK', index=2, number=2,
      options=None,
      type=None),
  ],
  containing_type=None,
  options=None,
  serialized_start=215,
  serialized_end=258,
)

I know workaround for this problem by extracting enum from message:

package tutorial;


enum PhoneType {
  MOBILE = 0;
  HOME = 1;
  WORK = 2;
}

message Person {
  required string name = 1;
  required int32 id = 2;
  optional string email = 3;

  message PhoneNumber {
    required string number = 1;
    optional PhoneType type = 2 [default = HOME];
  }

  repeated PhoneNumber phone = 4;
}

message AddressBook {
  repeated Person person = 1;
}

but maybe my issue is important for refactoring *.proto parser in PHP.

Good luck

My OS and ENV versions:

user@bieli:~$ uname -a
Linux desktop 3.5.0-30-generic #51-Ubuntu SMP Tue May 14 18:49:52 UTC 2013 i686 athlon i686 GNU/Linux

user@bieli:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 13.04
Release:    13.04
Codename:   raring

user@bieli:~$ php -v 
PHP 5.4.9-4ubuntu2.3 (cli) (built: Sep  4 2013 19:37:07) 
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2012 Zend Technologies

user@bieli:~$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/i686-linux-gnu/4.7/lto-wrapper
Target: i686-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.7.3-1ubuntu1' --with-bugurl=file:///usr/share/doc/gcc-4.7/README.Bugs --enable-languages=c,c++,go,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.7 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.7 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --with-system-zlib --enable-objc-gc --enable-targets=all --with-cloog --enable-cloog-backend=ppl --disable-cloog-version-check --disable-ppl-version-check --enable-multiarch --disable-werror --with-arch-32=i686 --with-multilib-list=m32,m64,mx32 --with-tune=generic --enable-checking=release --build=i686-linux-gnu --host=i686-linux-gnu --target=i686-linux-gnu
Thread model: posix
gcc version 4.7.3 (Ubuntu/Linaro 4.7.3-1ubuntu1) 

user@bieli:~$ phpize --version
Configuring for:
PHP Api Version:         20100412
Zend Module Api No:      20100525
Zend Extension Api No:   220100525

user@bieli:~$ dpkg -l | grep php5-dev
ii  php5-dev                                  5.4.9-4ubuntu2.3                       i386         Files for PHP5 module development

Hi @bieli please help me i want to install php-protobuf in xamp on window.but i have no idea how it will be installed .when i run protoc-php.php its show "requires protobuf extension installed to run". how can i install this extension ??

Thanks

@PunitSaharan I assume you know how to compile a extension, then your PHP version maybe too low. Because protoc-php.php uses namespaces, so PHP lower than 5.3 will run into this problem.

If not, then your problem has nothing to do with protobuf, but you do not know how to compile a PHP extension.

cd php-protobuf-master
phpize
./configure
make
sudo make install

vim /path/to/your/php.ini
add one line

extension=protobuf.so