本文實例講述了PHP中STDCLASS用法。分享給大家供大家參考,具體如下:
PHP中STDCLASS在我們開發應用中使用到的不多,但是PHP中STDCLASS作用是非常的大的,下面我們一起來看PHP中STDCLASS的用法.
在WordPress很多地方使用stdClass來定義一個物件(而通常是用數組的方式),然後使用get_object_vars來把定義的物件『轉換』成數組.
如下程式碼所示:
$tanteng = new stdClass(); $tanteng->name = 'tanteng'; $tanteng->email = 'xxx@qq.com'; $info = get_object_vars($tanteng); print_r($info); exit;
輸出:
Array ( [name] => tanteng [email] => xxx@qq.com )
$tanteng = array(); $tanteng['name'] = 'tanteng'; $tanteng['email'] = 'xxx@qq.com';
$user = new stdClass(); $user->name = 'gouki'; $user->hehe = 'hehe'; $myUser = $user; $myUser->name = 'flypig'; print_r($user); print_r($myUser); print_r($user);
stdClass Object ( [name] => flypig [hehe] => hehe ) stdClass Object ( [name] => flypig [hehe] => hehe ) stdClass Object ( [name] => flypig [hehe] => hehe )
$hehe['he1'] = 'he1'; $hehe['he2'] = 'he2'; $hh = (object) $hehe; print_r($hh);
stdClass Object ( [he1] => he1 [he2] => he2 )