get_object_vars()与转换成数组 [英] get_object_vars() vs. cast to array

查看:305
本文介绍了get_object_vars()与转换成数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有 get_object_vars之间的差异($ OBJ)(阵列)$ OBJ

似乎都返回对象的公共属性。

Both seem to return the public properties of the object.

哪家好?

推荐答案

这是不完全正确的。

get_object_vars 余地敏感,将返回所有的可见的特性除外的静态的无论其visbility的属性。如果您在类的外部调用它,你将只能得到公众成员;从派生类,你会得到protected和public成员;从类本身,你会得到所有成员。数组键重新present属性名,而不是错位。

get_object_vars is scope-sensitive and will return all visible properties excepting static properties regardless of their visbility. If you call it from outside your class, you'll only get the public members; from a derived class, you'll get the protected and public members; and from the class itself, you'll get all the members. The array keys represent the property names, and are not mangled.

(阵列)投的回报,至少在PHP 5.3.0,所有的对象属性,公众和其他。的属性的名称按照它们的保护水平错位

The (array) cast returns, at least on PHP 5.3.0, all the object properties, public and otherwise. The name of the properties are mangled according to their protection level:


  • 公共:不缺胳膊少腿,相同属性名

  • 保护:为属性键名称以a开头 *

  • 私人:财产密钥名称与类的名称开始

  • public: not mangled, identical to property names
  • protected: key name for property starts with a *
  • private: key name for property starts with the name of the class

请参阅转换为数组了解信息。

我希望你能更好地了解哪一个是最适合您的情况。

I hope you'll be able to better understand which one is the most appropriate for your situation.

这篇关于get_object_vars()与转换成数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆