chobie / php-protocolbuffers

PECL ProtocolBuffers
pecl.php.net/package/protocolbuffers
Other
128 stars 38 forks source link

No error when using a not initialized message #43

Open Nicien opened 10 years ago

Nicien commented 10 years ago

When using a get*() on a not initialized member of type 'message':

// proto message: message Foo { required int32 a = 1; }

message Container { optional Foo foo = 1; }

// php code: $c = new Container(); $c->getFoo()->setA(0); $c->serializeToString(); // no error, empty output. why ?

adding: $c->setFoo( new Foo() ); before calling getFoo() fix the problem...

I propose you to choose between two features:

The actual situation is disturbing.

chobie commented 10 years ago

Basically, getter method doesn't modify object. and it will return pseudo object when specified field does not initialized.

This specification is convenient for some kind of operation like below url. (I remember this spec came from c++ implementation.) https://github.com/chobie/protoc-gen-php/blob/master/src/protocolbuffers/generator/php/FileGenerator.php#L36

$c->serializeToString(); // no error, empty output. why ?

foo field has optional attribute. getter method doesn't change current message so it will output null value.

could you use mutableFoo() method instead of getFoo() if you want to modify uninitialized child message?

<?php
require "autoload.php";

$c = new Container();
$c->mutableFoo()->setA(0);

var_dump($c->serializeToString());

The actual situation is disturbing.

Certainly. How do you think about this solution?

Nicien commented 10 years ago

Tanks for your answer. mutableFoo() do exactly what i expected.

It follows the protocol buffer spec:

I almost forgot: your library is great ! The only thing missing is a ready to use package, or a tutorial. This tutorial could explain in one page how to install php-protocolbuffers + protoc-gen-php + compozer + use composer.