8

So, I am working on a project and I am having an issue as I keep getting both errors and warnings. I am quite new to PHP so be gentle. The program runs fine using PHP 5.5 However when I run the program in PHP 5.6 I receive several errors as follows;

[10-Oct-2016 10:04:46 America/Denver] PHP Warning: Erroneous data format for unserializing 'MMR\Bundle\CodeTyperBundle\Entity\User' in /hermes/bosnaweb14a/b1234/.../application/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php on line 833 [10-Oct-2016 10:04:46 America/Denver] PHP Notice: unserialize(): Error at offset 49 of 50 bytes in /hermes/bosnaweb14a/b1234/.../application/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php on line 833 [10-Oct-2016 10:04:46 America/Denver] PHP Fatal error: __clone method called on non-object in /hermes/bosnaweb14a/b1234/.../application/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php on line 837

Project Info

Platform: Symfony PHP Version: 5.6

Affected Code

public function newInstance()
{
    if ($this->_prototype === null) {
        if (PHP_VERSION_ID === 50429 || PHP_VERSION_ID === 50513) {
            $this->_prototype = $this->reflClass->newInstanceWithoutConstructor();
        } else {
           $this->_prototype = unserialize(sprintf('O:%d:"%s":0:{}', strlen($this->name), $this->name)); //Line 833
        }
    }

  return clone $this->_prototype; //Line 837
}

Any help would be greatly appreciated

Machavity
  • 30,841
  • 27
  • 92
  • 100
jamHud
  • 93
  • 1
  • 1
  • 8

2 Answers2

1

I tested:

var_dump(clone $t=unserialize(sprintf('O:%d:"%s":0:{}', strlen('name'), 'name')));

And it works.

You have to check the value of $this->name maybe its empty or has illegal chars.

Where is $this->name set in the first place?

JustOnUnderMillions
  • 3,741
  • 9
  • 12
0

Quick Fix: just add one more version i.e: PHP 5.6 for your if condition and it will be PHP_VERSION_ID === 50640 in your if condition

like:

    if ($this->_prototype === null) {
        if (PHP_VERSION_ID === 50429 || PHP_VERSION_ID === 50513 || PHP_VERSION_ID === 50640) {
            $this->_prototype = $this->reflClass>newInstanceWithoutConstructor();
        } else {
            $this->_prototype = unserialize(sprintf('O:%d:"%s":0:{}', strlen($this->name), $this->name));
        }
    }
Kashif Faraz Shamsi
  • 513
  • 1
  • 7
  • 21
  • This answer is a bit old, but I hope this does help someone in the future; the last two digits of the PHP version ID can change quickly; I am currently running 80121 on my local machine, but my docker environment is running 80120. It would be better to check on something like `PHP_VERSION_ID >= 50600 && PHP_VERSION_ID < 50700` for PHP 5.6, for instance. Even better, use the built-in [version_compare](https://www.php.net/manual/en/function.version-compare.php) function! – Fons Jul 24 '23 at 13:07