PHP Fatal PHP Fatal error: __clone method called on non-object
Asked Answered
P

2

9

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

Polymerism answered 11/10, 2016 at 15:53 Comment(4)
Do also test for is_object($this->_prototype), after the if or as second condition.Dickens
And your unserialize faild PHP Notice: unserialize(): may thats the problemDickens
There's obviously a string in $this->name with unescaped characters so the unserialize() function isn't able to decode it.Shelving
Are the standards for unserialize different between the php versions? As it runs fine using version 5.5 but fails in version 5.6Polymerism
D
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?

Dickens answered 11/10, 2016 at 16:7 Comment(1)
Thanks...I will look into thisPolymerism
C
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));
        }
    }
Coalfish answered 8/11, 2021 at 16:5 Comment(1)
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 function!Economist

© 2022 - 2024 — McMap. All rights reserved.