php - stdClass 与数组。推荐哪个?

标签 php mysql arrays stdclass

<分区>

推荐在哪里使用其中之一?我想存储数据库中列出的文章中的数据。

这是一个简单的问题:

echo $Datastore->name; //I like it works with foreach

//vs

echo $Datastore['name'];

哪个最好? stdClassarray 获取元素的速度有区别吗?

最佳答案

有一个类似的问题What is better stdClass or (object) array to store related data? with this answer

Based on small test (http://phpfiddle.org/lite/code/cz0-hyf) I can say that using "new stdClass()" is about 3 times slowlier than other options.

It is strange, but casting an array is done very efficiently compared to stdClass.

But this test meters only execution time. It does not meter memory.

P.S. I used phpFiddle only to share code. Test were done at my local PC.

In answer to another similar question你可以看到这个结论:

  1. For arrays, PHP 5.5 is faster than PHP 5.4, for object it is pretty much the same
  2. Class is slower than Arrays thanks to the optimization of PHP 5.5 and arrays.
  3. stdClass is evil.
  4. Class still uses less memory than Arrays. (about 30-40% less!!).
  5. SplFixedArray is similar to use a Class but it uses more memory.

关于php - stdClass 与数组。推荐哪个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24527618/

相关文章:

php - $_SERVER ["CONTENT_LENGTH"] 使用 XmlHttpRequest 上传文件时返回零

mysql - 多次计数,试图将它们分开,但它们相乘在一起

java - 关于使用字符串数组形式的值查询 Java Map。

c++ - 结构数组,其中包含数组。不会编译

mysql - 使用MySQL查询遍历行来制作递归树

php - 更改 is_numeric 以检查所有值

php - fatal error : Call to undefined function socket_create()

php - 在php中访问数组中的数据

php - 获取当前请求期间发生的所有 PHP 错误/警告/...

PHP多重过滤链接