DDC-3120: Warning: Erroneous data format for unserializing PHP5.6+ #3872

Open
opened 2026-01-22 14:29:43 +01:00 by admin · 0 comments
Owner

Originally created by @doctrinebot on GitHub (May 11, 2014).

Originally assigned to: @Ocramius on GitHub.

Jira issue originally created by user techkey:

Hi all,

There seems to be something strange going on in the method newInstance() of the class \Doctrine\ORM\Mapping\ClassMetadataInfo.

The original class method looks like this:

{quote}\Doctrine\ORM\Mapping\ClassMetadataInfo#newInstance(){quote}

    {
        if ($this->_prototype === null) {
            $this->_prototype = unserialize(sprintf('O:%d:"%s":0:{}', strlen($this->name), $this->name));
        }

        return clone $this->_prototype;
    }

What happens now when a class that implements \Serializable is that a "Warning: Erroneous data format for unserializing" shows up and the function unserialize() returns false.

That is because a class that implements \Serializable is expected to have the letter 'C' in the serialize string instead of the letter 'O'.

I've made a quick work-around like this:

{quote}\Doctrine\ORM\Mapping\ClassMetadataInfo#newInstance(){quote}

    {
        if ($this->_prototype === null) {
            $this->_prototype = @unserialize(sprintf('O:%d:"%s":0:{}', strlen($this->name), $this->name));
            if ($this->_prototype === false) {
                $this->_prototype = unserialize(sprintf('C:%d:"%s":0:{}', strlen($this->name), $this->name));
            }
        }

        return clone $this->_prototype;
    }

That seems to work in my isolated tests and with Symfony2 and Doctrine2 and FOSUserBundle together.

I've noticed this because the Model\User class from FOSUserBundle implements \Serializable.

I had to implement a check in Model\User class because when using {{'C:%d:"%s":0:{}'}} the $serialized parameter of the unserialize method in the Model\User class is a empty string then.

That warning seems only to happen with PHP5.6+. PHP5.5.12 and below doesn't show that warning.

I hope someone can shine some light on this, thank you,

Cornelis.

Originally created by @doctrinebot on GitHub (May 11, 2014). Originally assigned to: @Ocramius on GitHub. Jira issue originally created by user techkey: Hi all, There seems to be something strange going on in the method `newInstance()` of the class `\Doctrine\ORM\Mapping\ClassMetadataInfo`. The original class method looks like this: {quote}\Doctrine\ORM\Mapping\ClassMetadataInfo#newInstance(){quote} ``` { if ($this->_prototype === null) { $this->_prototype = unserialize(sprintf('O:%d:"%s":0:{}', strlen($this->name), $this->name)); } return clone $this->_prototype; } ``` What happens now when a class that implements \Serializable is that a "Warning: Erroneous data format for unserializing" shows up and the function unserialize() returns false. That is because a class that implements \Serializable is expected to have the letter 'C' in the serialize string instead of the letter 'O'. I've made a quick work-around like this: {quote}\Doctrine\ORM\Mapping\ClassMetadataInfo#newInstance(){quote} ``` { if ($this->_prototype === null) { $this->_prototype = @unserialize(sprintf('O:%d:"%s":0:{}', strlen($this->name), $this->name)); if ($this->_prototype === false) { $this->_prototype = unserialize(sprintf('C:%d:"%s":0:{}', strlen($this->name), $this->name)); } } return clone $this->_prototype; } ``` That seems to work in my isolated tests and with Symfony2 and Doctrine2 and FOSUserBundle together. I've noticed this because the `Model\User` class from FOSUserBundle implements `\Serializable`. I had to implement a check in Model\User class because when using {{'C:%d:"%s":0:{}'}} the $serialized parameter of the `unserialize` method in the `Model\User` class is a empty string then. That warning seems only to happen with PHP5.6+. PHP5.5.12 and below doesn't show that warning. I hope someone can shine some light on this, thank you, Cornelis.
admin added the Bug label 2026-01-22 14:29:43 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#3872