Skip to content

[PHP] Problem with serializing Proto objects using igbinary #17289

Open
@Keleset-Web

Description

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: #14872, #12714, #12718

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions