我正在使用工厂创建一个对象,并使用静态方法来反序列化此对象:
public static function factory($idText) {
$fetchedObject = self::fetchStoredObject($idText);
return $fetchedObject;
}
private static function fetchStoredObject($idText) {
$fetchedText = DB::select()
->from(table)
->where('idText', '=', $idText)
->execute()->as_array();
if (!empty($fetchedText)) {
return unserialize(base64_decode($fetchedText[0]['txtContent']));
} else {
return NULL;
}
}
对象以这种方式创建:
$text = Article::factory($idText);
但是我收到以下错误:
unserialize() [<a href='function.unserialize'>function.unserialize</a>]:
Function spl_autoload_call() hasn't defined the class it was called for
在以下fetchStoredObject
方法行上:
return unserialize(base64_decode($fetchedText[0]['txtContent']));
为什么会发生此错误?
编辑
我的类具有以下结构:
class Article {
private $phpMorphy;
private $words; //contains instances of class Word
...
private function __construct($idText) {
$this->initPhpMorphy(); // $this->phpMorphy gets reference to the object here
}
public function __sleep() {
return Array(
'rawText',
'idText',
'properties',
'words',
'links'
);
}
public function __wakeup() {
$this->initPhpMorphy();
}
}
Word
类不包含对 phpMorphy 的引用作为自己的属性,而是在其方法中将其用作函数参数。下面是序列化字符串的一部分:
" Article words";a:107:{i:0;O:4:"Word":10:{s:5:" * id";O:9:"phpMorphy":7:{s:18:" * storage_factory";O:25:
看起来 phpMorphy 是用 connectrion 序列化到 Word 类的。我说的对吗?
发生此错误是因为在序列化字符串中存在对尚未包含的类的引用 - 因此触发 PHP 自动加载机制以加载该类,并且由于某种原因失败。
调试步骤如下:
- 标识该序列化字符串中包含的类。
- 检查某处是否有该类的代码。
- 确保可以通过自动加载加载此代码。或者,请确保在反序列化之前包含代码。
你用的是哪个版本的 PHP?您在哪里存储了文章类?尝试手动要求()。
由于 Sven 的建议,该问题已得到解决。类 Word 的对象(类 Article的一部分)包含对 phpMorphy 类的引用(这是因为我在创建单词的实例时更改了参数顺序!