
用途:
1.stdClass通過(guò)調(diào)用它們直接訪(fǎng)問(wèn)成員。
2.它在動(dòng)態(tài)對(duì)象中很有用。
3.它用于設(shè)置動(dòng)態(tài)屬性等。
程序1:使用數(shù)組存儲(chǔ)數(shù)據(jù)
?php // 定義一個(gè)數(shù)組employee $employee_detail_array = array( name = John Doe , position = Software Engineer , address = 53, nth street, city , status = best // 顯示內(nèi)容print_r($employee_detail_array); ?
輸出:
Array [name] = John Doe [position] = Software Engineer [address] = 53, nth street, city [status] = best)
程序2:使用stdClass而不是數(shù)組來(lái)存儲(chǔ)員工詳細(xì)信息(動(dòng)態(tài)屬性)
?php // 定義employee對(duì)象樣式$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 // 顯示內(nèi)容 print_r($employee_object); ?
輸出:
stdClass Object [name] = John Doe [position] = Software Engineer [address] = 53, nth street, city [status] = Best)
注意:可以將數(shù)組類(lèi)型轉(zhuǎn)換為對(duì)象,將對(duì)象轉(zhuǎn)換為數(shù)組。
程序3:將數(shù)組轉(zhuǎn)換為對(duì)象
?php $employee_detail_array = array( name = John Doe , position = Software Engineer , address = 53, nth street, city , status = best // 從數(shù)組到對(duì)象的類(lèi)型轉(zhuǎn)換$employee = (object) $employee_detail_array; print_r($employee); ?
輸出:
Array [name] = John Doe [position] = Software Engineer [address] = 53, nth street, city [status] = Best)
本篇文章就是關(guān)于PHP中stdClass的介紹,希望對(duì)需要的朋友有所幫助!
以上就是PHP中的stdClass是什么的詳細(xì)內(nèi)容,PHP教程
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注