2013-10-22 97 views
2

我使用工廠創建對象和靜態方法來解序列化對象的類:反序列化()...功能spl_autoload_call()沒有定義它被稱爲爲

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類。我對嗎?

回答

0

您使用的是什麼版本的PHP?你在哪裏存儲你的文章類?嘗試手動要求()。

4

發生該錯誤是因爲在序列化的字符串中存在對尚未包含的類的引用 - 所以PHP自動加載機制被觸發以加載該類,並且由於某種原因失敗。

您的調試步驟是:

  1. 確定哪些類是包含在序列化的字符串。
  2. 檢查你的代碼是否在某個地方。
  3. 確保此代碼可以通過自動加載加載。或者,確保在反序列化之前包含代碼。
+0

導致問題的類名是phpMorphy。但是該對象的引用存儲在私有變量中。 '_sleep'函數返回的數組中不包含該變量。那麼爲什麼這個變量被序列化呢? –

+0

顯然,這個問題的背景比你第一次告訴我們的要多。你不會簡單地用一個對象序列化某些東西,但是你也可以使用'_sleep' - 可能是'__wakeup'。這些方法可能包含解釋您的問題的代碼,因此請更新您的問題並提供更多信息。 – Sven

+0

另外,與我們分享序列化的字符串(密切關注那裏的機密信息!)。 – Sven

0

由於Sven的建議,問題得到解決。 Word類(類文章的一部分)的對象包含對phpMorphy類的引用(發生這種情況是因爲我在創建單詞實例時更改了參數順序!)。

相關問題