protobuf icon indicating copy to clipboard operation
protobuf copied to clipboard

[PHP] Problem with serializing Proto objects using igbinary

Open Keleset-Web opened this issue 1 year ago • 6 comments

I am using PHP 8.3 with the ext-protobuf 3.21.1 extension in the project. OS: Linux

The problem is that when serializing a generated PHP object based on a Protobuf Message, its content is not read. Serialization in the project is performed using igbinary_serialize.

I have two example code fragments: one interacts with a Protobuf object (which inherits from \Google\Protobuf\Internal\Message), and the other interacts with an object created from a standard PHP class. In the first case, igbinary cannot read the value set in the Protobuf object during serialization, whereas in the second case, serialization proceeds without any issues.

First case:

<?php

declare(strict_types=1);

$timestamp = new Google\Protobuf\Timestamp();
$timestamp->setSeconds(1719822351);

$serializedData = igbinary_serialize($timestamp);

echo 'Igbinary serialized data: '.$serializedData.PHP_EOL;
echo 'Set seconds value in object: '.$timestamp->getSeconds().PHP_EOL;

Result:

Igbinary serialized data: Google\Protobuf\Timestamp
Set seconds value in object: 1719822351

Second case:

<?php

declare(strict_types=1);

class Timestamp
{
    protected int $seconds = 0;

    public function setSeconds(int $seconds): void
    {
        $this->seconds = $seconds;
    }

    public function getSeconds(): int
    {
        return $this->seconds;
    }
}

$timestamp = new Timestamp();
$timestamp->setSeconds(1719822351);
$serializedData = igbinary_serialize($timestamp);

echo 'Igbinary serialized data: '.$serializedData.PHP_EOL;
echo 'Set seconds value in object: '.$timestamp->getSeconds().PHP_EOL;

Result:

Igbinary serialized data:       Timestamp
*seconds
f�h
Set seconds value in object: 1719822351

Can I achieve the correct serialization result as shown in the second example?

Similar topics: https://github.com/protocolbuffers/protobuf/issues/14872, https://github.com/protocolbuffers/protobuf/issues/12714, https://github.com/protocolbuffers/protobuf/pull/12718

Keleset-Web avatar Jul 01 '24 09:07 Keleset-Web

We triage inactive PRs and issues in order to make it easier to find active work. If this issue should remain active or becomes active again, please add a comment.

This issue is labeled inactive because the last activity was over 90 days ago. This issue will be closed and archived after 14 additional days without activity.

github-actions[bot] avatar Sep 30 '24 10:09 github-actions[bot]

Hello, the problem is still relevant, I would like to figure it out and find a solution

Keleset-Web avatar Sep 30 '24 10:09 Keleset-Web

We triage inactive PRs and issues in order to make it easier to find active work. If this issue should remain active or becomes active again, please add a comment.

This issue is labeled inactive because the last activity was over 90 days ago. This issue will be closed and archived after 14 additional days without activity.

github-actions[bot] avatar Dec 30 '24 10:12 github-actions[bot]

This issue is still active

allsilaevex avatar Jan 03 '25 06:01 allsilaevex

We triage inactive PRs and issues in order to make it easier to find active work. If this issue should remain active or becomes active again, please add a comment.

This issue is labeled inactive because the last activity was over 90 days ago. This issue will be closed and archived after 14 additional days without activity.

github-actions[bot] avatar Apr 04 '25 10:04 github-actions[bot]

This issue is still active

allsilaevex avatar Apr 08 '25 08:04 allsilaevex

This seems to be an extension issue; the properties of the object are not available (if you do a var_dump($timestamp) you will see no property either)

This is a problem for caching or debugging

Can we make proto serializable and natively json serializable as well?

Tofandel avatar May 27 '25 23:05 Tofandel

We triage inactive PRs and issues in order to make it easier to find active work. If this issue should remain active or becomes active again, please add a comment.

This issue is labeled inactive because the last activity was over 90 days ago. This issue will be closed and archived after 14 additional days without activity.

github-actions[bot] avatar Sep 21 '25 10:09 github-actions[bot]

We triage inactive PRs and issues in order to make it easier to find active work. If this issue should remain active or becomes active again, please reopen it.

This issue was closed and archived because there has been no new activity in the 14 days since the inactive label was added.

github-actions[bot] avatar Oct 06 '25 10:10 github-actions[bot]