用途:
1.stdClass通過調用它們直接訪問成員。
2.它在動態對象中很有用。
3.它用于設置動態屬性等。
程序1:使用數組存儲數據
?php // 定義一個數組employee $employee_detail_array = array( name = John Doe , position = Software Engineer , address = 53, nth street, city , status = best // 顯示內容print_r($employee_detail_array); ?
輸出:
Array [name] = John Doe [position] = Software Engineer [address] = 53, nth street, city [status] = best)
程序2:使用stdClass而不是數組來存儲員工詳細信息(動態屬性)
?php // 定義employee對象樣式$employee_object = new stdClass; $employee_object- name = John Doe $employee_object- position = Software Engineer $employee_object- address = 53, nth street, city $employee_object- status = Best // 顯示內容 print_r($employee_object); ?
輸出:
stdClass Object [name] = John Doe [position] = Software Engineer [address] = 53, nth street, city [status] = Best)
注意:可以將數組類型轉換為對象,將對象轉換為數組。
程序3:將數組轉換為對象
?php $employee_detail_array = array( name = John Doe , position = Software Engineer , address = 53, nth street, city , status = best // 從數組到對象的類型轉換$employee = (object) $employee_detail_array; print_r($employee); ?
輸出:
Array [name] = John Doe [position] = Software Engineer [address] = 53, nth street, city [status] = Best)
本篇文章就是關于PHP中stdClass的介紹,希望對需要的朋友有所幫助!
以上就是PHP中的stdClass是什么的詳細內容,PHP教程
鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。
新聞熱點
疑難解答