搜尋
首頁後端開發php教程php中的Unserialize與Autoload

  1. $string = 'O:6:「Foobar」:2:{s:3:「foo」; s:1:「1」;s:3:「bar」;s:1:「2」;}';

  2. $result = unserialize($string);
  3. var_dump($result); p>
  4. /*

  5. object(__PHP_Incomplete_Class)[1]
  6. public '__PHP_Incomplete_Class_Name' => string 'Foobar' (length=6)
  7. public 'foo' => string 'foo'1 ' (length=1)
  8. public 'bar' => string '2' (length=1)
  9. */
  10. ?>
複製程式碼

當反序列化一個物件時,如果物件的類別定義不存在,那麼PHP會引入一個未完成類別的概念,即:__PHP_Incomplete_Class,此時雖然我們反序列化成功了,但還是無法存取物件中的數據,否則會出現如下報錯訊息: The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition of the object you are trying to operate on was loaded _before_ unserialize() sload class class sloaded a. definition.

這不是什麼難事兒,只要做一次強制型別轉換,變成陣列就好了:

  1. $string = 'O:6:「Foobar」:2:{s:3:「 foo」;s:1:「1」;s:3:「bar」;s:1:「2」;}';

  2. $result = (array)unserialize($string);
  3. var_dump( $result);
  4. /*

  5. array
  6. '__PHP_Incomplete_Class_Name' => string 'Foobar' (length=6)
  7. 'foo' => string '1' ( length=1)
  8. 'bar' => string '2' (length=1)
  9. */
  10. ?>
複製程式碼

複製程式碼
    不過如果系統啟動了Autoload,情況會變得複雜些。順便插句話:PHP其實提供了一個名為unserialize_callback_func配置選項,但意思和autoload差不多,這裡就不介紹了,咱們就說autoload,例子如下:
  1. spl_autoload_register(function($name) {
  2. var_dump($name);
  3. });
  4. $string = 'O:6:「Foobar」:2:{s:3:「foo」;s:1:「1」;s:3:「bar」;s:1:「2」;}';
$result = (array)unserialize($string);
var_dump($result);?>

複製代碼
    執行上面程式碼會發現,spl_autoload_register被觸發了,多數時候這是有意義的,但如果遇到一個定義不當的spl_autoload_register,就悲催了,比如說下面這段程式碼:
  1. spl_autoload_register(function($name) {
  2. include “/path/to/{$name}.php”;
  3. });
$string = 'O:6:「Foobar」:2:{s:3:「foo」;s:1:「1」;s:3:「bar」;s: 1:「2」;}';
$result = (array)unserialize($string);var_dump($result);?>

複製程式碼
    毫無疑問,因為找不到類別定義文件,所以報錯了!改改spl_autoload_register肯定行,但前提是你能改,如果涉及第三方代碼,我們就不能擅自做主了,此時我們需要一種方法讓unserialize能繞開autoload,最簡單的方法是把我們需要的類FAKE出來:
  1. spl_autoload_register(function($name) {
  2. include “/path/to/{$name}.php”;
  3. });
  4. class Foobar {} // Oh, Shit!
  5. $string = 'O:6:「Foobar」:2:{s:3:「foo」;s:1:「1」;s:3:「bar」;s:1:「2」; }';
$result = (array)unserialize($string);
var_dump($result);?>

複製代碼
    不得不說,上面的程式碼真的很垃圾。提供大家一人我寫的:
  1. spl_autoload_register(function($name) {

  2. include “/path/to/{$ name}.php」;
  3. });
  4. $string = 'O:6:「Foobar」:2:{s:3:「foo」;s:1:「 1」;s:3:「bar」;s:1:「2」;}';

  5. $functions = spl_autoload_functions();
  6. foreach ($functions as $function) {
  7. spl_autoload_unregister($ function);
  8. }
  9. $result = (array)unserialize($string);

  10. foreach ($functions as $function) {

  11. spl_autoload_register($function);
  12. }
var_dump($result);
?>
複製代碼

程式碼雖然多了點,但至少沒有FAKE類,看起來是不是舒服多了。



陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
PHP中的依賴注入:避免常見的陷阱PHP中的依賴注入:避免常見的陷阱May 16, 2025 am 12:17 AM

DependencyInjection(DI)inPHPenhancescodeflexibilityandtestabilitybydecouplingdependencycreationfromusage.ToimplementDIeffectively:1)UseDIcontainersjudiciouslytoavoidover-engineering.2)Avoidconstructoroverloadbylimitingdependenciestothreeorfour.3)Adhe

如何加快PHP網站:性能調整如何加快PHP網站:性能調整May 16, 2025 am 12:12 AM

到Improveyourphpwebsite的實力,UsEthestertate:1)emplastOpCodeCachingWithOpcachetCachetOspeedUpScriptInterpretation.2)優化的atabasequesquesquesquelies berselectingOnlynlynnellynnessaryfields.3)usecachingsystemssslikeremememememcachedisemcachedtoredtoredtoredsatabaseloadch.4)

通過PHP發送大規模電子郵件:有可能嗎?通過PHP發送大規模電子郵件:有可能嗎?May 16, 2025 am 12:10 AM

是的,ItispossibletosendMassemailswithp.1)uselibrarieslikeLikePhpMailerorSwiftMailerForeffitedEmailsending.2)enasledeLaysBetenemailstoavoidSpamflagssspamflags.3))

PHP中依賴注入的目的是什麼?PHP中依賴注入的目的是什麼?May 16, 2025 am 12:10 AM

DependencyInjection(DI)inPHPisadesignpatternthatachievesInversionofControl(IoC)byallowingdependenciestobeinjectedintoclasses,enhancingmodularity,testability,andflexibility.DIdecouplesclassesfromspecificimplementations,makingcodemoremanageableandadapt

如何使用PHP發送電子郵件?如何使用PHP發送電子郵件?May 16, 2025 am 12:03 AM

使用PHP發送電子郵件的最佳方法包括:1.使用PHP的mail()函數進行基本發送;2.使用PHPMailer庫發送更複雜的HTML郵件;3.使用SendGrid等事務性郵件服務提高可靠性和分析能力。通過這些方法,可以確保郵件不僅到達收件箱,還能吸引收件人。

如何計算PHP多維數組的元素總數?如何計算PHP多維數組的元素總數?May 15, 2025 pm 09:00 PM

計算PHP多維數組的元素總數可以使用遞歸或迭代方法。 1.遞歸方法通過遍歷數組並遞歸處理嵌套數組來計數。 2.迭代方法使用棧來模擬遞歸,避免深度問題。 3.array_walk_recursive函數也能實現,但需手動計數。

PHP中do-while循環有什麼特點?PHP中do-while循環有什麼特點?May 15, 2025 pm 08:57 PM

在PHP中,do-while循環的特點是保證循環體至少執行一次,然後再根據條件決定是否繼續循環。 1)它在條件檢查之前執行循環體,適合需要確保操作至少執行一次的場景,如用戶輸入驗證和菜單系統。 2)然而,do-while循環的語法可能導致新手困惑,且可能增加不必要的性能開銷。

PHP中如何哈希字符串?PHP中如何哈希字符串?May 15, 2025 pm 08:54 PM

在PHP中高效地哈希字符串可以使用以下方法:1.使用md5函數進行快速哈希,但不適合密碼存儲。 2.使用sha256函數提高安全性。 3.使用password_hash函數處理密碼,提供最高安全性和便捷性。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境