Aldibe
Aldibe

Reputation: 1347

PHP protobuf error - Undefined method for encode/decode

I'm trying to learn about Protobuf in PHP using https://github.com/google/protobuf/tree/master/php . Currently I'm stuck in an error.

My steps to install protobuf:

Below is my code:

The protoc command resulted in two file, APIReq.php and GPBMetadata/MsgFormat.php

After that, I added require_once __DIR__ . '/vendor/autoload.php'; and require_once __DIR__ . '/GPBMetadata/MsgFormat.php'; in the generated PHP file because when I ran php APIReq.php it came up with

    PHP Fatal error:  Class 'Google\Protobuf\Internal\Message' not found in /var/www/html/testing/APIReq.php on line 13

After I added those line, the error disappeared, so I assume both line fixed the problem

When I run the PHP code, it returns error message:

PHP Fatal error:  Call to undefined method APIReq::encode()

How can I fix this?

Edit: Tried this with protobuf 3.3.0 as well, with same result.

Upvotes: 2

Views: 3222

Answers (1)

Ahmed Bebars
Ahmed Bebars

Reputation: 367

Encode & Decode not exist in the codebase as I traced down.

This change was introduced in 3.3.0

//to encode message 
$data = $param->serializeToString();

//to decode message
$param2 = new APIReq();
$param2->mergeFromString($data);

Upvotes: 6

Related Questions