PHP中stdClass的意义
来源:互联网 发布:中文数据库有哪些 编辑:程序博客网 时间:2024/05/29 21:18
在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 )
get_object_vars的作用是返回由对象属性组成的关联数组。它的效果跟这样定义数组其实是一样的:
$tanteng = array();$tanteng['name'] = 'tanteng';$tanteng['email'] = 'xxx@qq.com';
可以这样理解:stdClass是一个内置类,它没有成员变量,也没有成员方法的一个类。new一个stdClass就是实例化了一个『空』对象,它本身没什么意义,但是用stdClass定义有什么好处呢?
如下代码:
$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 )
阅读全文
0 0
- PHP中stdClass的意义
- Php中StdClass、object、array的概念
- php中stdClass的用法分析
- php中stdClass的用法分析
- php的stdClass总结
- PHP中把stdClass Object转array的几个方法
- PHP中把stdClass Object转array的几个方法
- PHP中把stdClass Object转array的几个方法
- php中把stdClass Object转array的几个方法
- PHP中把stdClass Object转array的几个方法
- PHP中stdClass简介及 stdClass Object转array方法
- php stdClass
- PHP中遍历stdclass object 及 json
- PHP中遍历stdclass object 及 json
- PHP中遍历stdclass object 及 json
- stdClass是PHP的一个基类
- PHP stdclass转array的方法
- php中$_SERVER的意义
- 六、入侵Activiti-SpringBoot使用代码动态编辑流程文件
- IAR无法打印浮点的解决方法
- B
- Jeesite移除list-,view-等前缀,方便SEO
- 反转链表,(5)
- PHP中stdClass的意义
- js 的Object.assign(),要慎用
- 树莓派使用小记
- [LeetCode] 526. Beautiful Arrangement
- Rxjava学习
- 新生多校第六场 插塔憋憋乐(数论+贪心)
- bzoj4989 [Usaco2017 Feb]Why Did the Cow Cross the Road(树状数组求逆序对个数)
- CMake+VS2013编译与安装32/64位PCL1.8RC1源码
- css样式之display属性----table和block的区别