PHP致命错误PHP致命错误:__clone方法在非对象上呼叫

ep6jt1vc  于 2022-12-10  发布在  PHP
关注(0)|答案(2)|浏览(223)

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

wooyq4lh

wooyq4lh1#

我试探道:
var_dump(clone $t=unserialize(sprintf('O:%d:"%s":0:{}', strlen('name'), 'name')));
而且很管用。
您必须检查$this->name的值,可能为空或包含非法字符。
$this->name首先设置在哪里?

pbpqsu0x

pbpqsu0x2#

快速修复:只需再添加一个版本,即:PHP 5.6,在if条件中它将是PHP_VERSION_ID === 50640
例如:

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));
        }
    }

相关问题