使用数组VS对象存储数据[重复]

时间:2020-12-05 16:56:11

Possible Duplicate:
When should I use stdClass and when should I use an array in php5 oo code ??

可能重复:我什么时候应该使用stdClass什么时候应该在php5 oo代码中使用数组?

What are the benefits of using one of the two structures over the other?

使用两种结构中的一种比另一种结构有什么好处?

// array
$user['name'] = 'Emanuil';

// object
$user->name = 'Emanuil';

3 个解决方案

#1


31  

Arrays

  • There are tons of array_* functions that can work on arrays, most of which are very fast.
  • 有大量的array_ *函数可以在数组上运行,其中大多数都非常快。

  • By default they passes by value (copied around)
  • 默认情况下,它们按值传递(复制)

  • Lightweight/Simple (changes only effect local variable, less to think about)
  • 轻量级/简单(仅更改效果局部变量,更少考虑)

  • Often used for build once data (data that doesn't change)
  • 通常用于构建一次数据(数据不会更改)

  • All data is public
  • 所有数据都是公开的

  • Slightly less resource intensive
  • 资源密集程度略低

Objects

  • Methods can be used to keep the data stricter. (IE. checks that a field fits a format)
  • 可以使用方法来保持数据更严格。 (IE。检查字段是否适合格式)

  • Subclassing (reducing code duplication)
  • 子类化(减少代码重复)

  • By default they are passed by reference
  • 默认情况下,它们通过引用传递

  • Changes to data can have cascading effects (__get, __set, etc)
  • 对数据的更改可能会产生级联效果(__ get,__ set等)

  • Often used for data that is more mutable
  • 通常用于更可变的数据

  • Can protect data from outside via functions and protected/private variables
  • 可以通过函数和受保护/私有变量保护外部数据

  • Function type hinting of objects is more flexible (different typehint for different classes)
  • 对象的函数类型提示更灵活(不同类的不同类型提示)

#2


7  

Just run a simple test:

只需运行一个简单的测试:

$ts_o = microtime(true);
for($i=0;$i<=1000;$i++)
{
    new stdClass();
}
$total_object = microtime(true) - $ts_o;

Versus:

$ts_a = microtime(true);
for($i=0;$i<=1000;$i++)
{
    array();
}
$total_array = microtime(true) - $ts_a;

And calculate the he results.

并计算他的结果。

echo 'Object: ' . $total_object . ' / Array: ' . $total_array;

Results: Object: 0.002635 / Array: 0.001243

结果:对象:0.002635 /数组:0.001243

As you can see that Arrays are faster in regards to speed, average 46.6% infact.

正如您所看到的,Arrays在速度方面更快,平均值为46.6%。

But when you start adding variables they suddenly turn around:

但是当你开始添加变量时,他们会突然转身:

$ts_o = microtime(true);
for($i=0;$i<=1000;$i++)
{
    $var = new stdClass();
    $var->booleon = true;
}
$total_object = microtime(true) - $ts_o;
unset($var);

$ts_a = microtime(true);
for($i=0;$i<=1000;$i++)
{
    $var = array();
    $var['booleon'] = true;
}
$total_array = microtime(true) - $ts_a;

echo 'Object: ' . ($total_object)  . ' / Array: ' . $total_array;

New Results: 0.0037809 / Array: 0.0046189

新结果:0.0037809 /数组:0.0046189

There's a few test you would have to do then find your mean / mode at the end of the test to find the one that truly is the better entity.

您需要进行一些测试,然后在测试结束时找到您的平均值/模式,找到真正更好的实体。

You can do a test on memory by doing a memory_get_usage : http://php.net/manual/en/function.memory-get-usage.php with the same principles.

您可以通过执行memory_get_usage来对内存进行测试:http://php.net/manual/en/function.memory-get-usage.php具有相同的原则。

#3


5  

What are you doing? Neither structure is better at everything, which is why both exist. So it really depends on your problem set which is "better". And even then, it may simply be a case of which you prefer. A lot of architecture and code design is personal preference and style.

你在做什么?这两种结构都不是更好,这就是为什么两者都存在的原因。所以它真的取决于你的问题集“更好”。即便如此,它可能只是您喜欢的情况。许多架构和代码设计都是个人偏好和风格。

Now, in general, there is only one time I would use a method-less object (stdclass, or a custom defined one) over an array. That's if I need to pass that data around a lot and modify it in multiple places. With an array, you'll need to pass it by reference, which gets cumbersome and can introduce sources of error... With an object, it's passed as an object reference by default (not a variable reference, but a pointer to the same object). So with an array you'd need to do something like:

现在,通常,我只有一次在数组上使用无方法对象(stdclass或自定义的对象)。如果我需要经常传递那些数据并在多个地方修改它。使用数组,你需要通过引用传递它,这会很麻烦,并且可能会引入错误源...对于一个对象,它默认传递为对象引用(不是变量引用,而是指向它的指针)目的)。因此,对于数组,您需要执行以下操作:

function doSomething($inVar, array &$inOutArray) {
    $inOutArray['bar'] = 'baz';
}

For each function that modifies (or might modify) the array.

对于修改(或可能修改)数组的每个函数。

Whereas with an object, you could just do:

而对于一个对象,你可以这样做:

function doSomething($inVar, $object) {
    $object->bar = 'baz';
}

It's shorter (sure, only one character, but it's one character everywhere you might want to modify the array). It's less prone to bugs, since if you later do $object = new Something(), it won't change the original object (since it's not a variable reference)...

它更短(当然,只有一个字符,但它可能是你想要修改数组的一个字符)。它不容易出错,因为如果你以后做$ object = new Something(),它不会改变原始对象(因为它不是变量引用)...

The only argument is that it's slightly less readable, since the & shows you explicitly that you intend to modify that input variable. But if you understand how objects work in PHP 5, you shouldn't be caught off guard (and hence it's an acceptable tradeoff in my mind)...

唯一的论点是它的可读性稍差,因为&明确表示您打算修改该输入变量。但是如果你理解PHP 5中的对象如何工作,你就不应该措手不及(因此在我看来这是一个可以接受的权衡)......

#1


31  

Arrays

  • There are tons of array_* functions that can work on arrays, most of which are very fast.
  • 有大量的array_ *函数可以在数组上运行,其中大多数都非常快。

  • By default they passes by value (copied around)
  • 默认情况下,它们按值传递(复制)

  • Lightweight/Simple (changes only effect local variable, less to think about)
  • 轻量级/简单(仅更改效果局部变量,更少考虑)

  • Often used for build once data (data that doesn't change)
  • 通常用于构建一次数据(数据不会更改)

  • All data is public
  • 所有数据都是公开的

  • Slightly less resource intensive
  • 资源密集程度略低

Objects

  • Methods can be used to keep the data stricter. (IE. checks that a field fits a format)
  • 可以使用方法来保持数据更严格。 (IE。检查字段是否适合格式)

  • Subclassing (reducing code duplication)
  • 子类化(减少代码重复)

  • By default they are passed by reference
  • 默认情况下,它们通过引用传递

  • Changes to data can have cascading effects (__get, __set, etc)
  • 对数据的更改可能会产生级联效果(__ get,__ set等)

  • Often used for data that is more mutable
  • 通常用于更可变的数据

  • Can protect data from outside via functions and protected/private variables
  • 可以通过函数和受保护/私有变量保护外部数据

  • Function type hinting of objects is more flexible (different typehint for different classes)
  • 对象的函数类型提示更灵活(不同类的不同类型提示)

#2


7  

Just run a simple test:

只需运行一个简单的测试:

$ts_o = microtime(true);
for($i=0;$i<=1000;$i++)
{
    new stdClass();
}
$total_object = microtime(true) - $ts_o;

Versus:

$ts_a = microtime(true);
for($i=0;$i<=1000;$i++)
{
    array();
}
$total_array = microtime(true) - $ts_a;

And calculate the he results.

并计算他的结果。

echo 'Object: ' . $total_object . ' / Array: ' . $total_array;

Results: Object: 0.002635 / Array: 0.001243

结果:对象:0.002635 /数组:0.001243

As you can see that Arrays are faster in regards to speed, average 46.6% infact.

正如您所看到的,Arrays在速度方面更快,平均值为46.6%。

But when you start adding variables they suddenly turn around:

但是当你开始添加变量时,他们会突然转身:

$ts_o = microtime(true);
for($i=0;$i<=1000;$i++)
{
    $var = new stdClass();
    $var->booleon = true;
}
$total_object = microtime(true) - $ts_o;
unset($var);

$ts_a = microtime(true);
for($i=0;$i<=1000;$i++)
{
    $var = array();
    $var['booleon'] = true;
}
$total_array = microtime(true) - $ts_a;

echo 'Object: ' . ($total_object)  . ' / Array: ' . $total_array;

New Results: 0.0037809 / Array: 0.0046189

新结果:0.0037809 /数组:0.0046189

There's a few test you would have to do then find your mean / mode at the end of the test to find the one that truly is the better entity.

您需要进行一些测试,然后在测试结束时找到您的平均值/模式,找到真正更好的实体。

You can do a test on memory by doing a memory_get_usage : http://php.net/manual/en/function.memory-get-usage.php with the same principles.

您可以通过执行memory_get_usage来对内存进行测试:http://php.net/manual/en/function.memory-get-usage.php具有相同的原则。

#3


5  

What are you doing? Neither structure is better at everything, which is why both exist. So it really depends on your problem set which is "better". And even then, it may simply be a case of which you prefer. A lot of architecture and code design is personal preference and style.

你在做什么?这两种结构都不是更好,这就是为什么两者都存在的原因。所以它真的取决于你的问题集“更好”。即便如此,它可能只是您喜欢的情况。许多架构和代码设计都是个人偏好和风格。

Now, in general, there is only one time I would use a method-less object (stdclass, or a custom defined one) over an array. That's if I need to pass that data around a lot and modify it in multiple places. With an array, you'll need to pass it by reference, which gets cumbersome and can introduce sources of error... With an object, it's passed as an object reference by default (not a variable reference, but a pointer to the same object). So with an array you'd need to do something like:

现在,通常,我只有一次在数组上使用无方法对象(stdclass或自定义的对象)。如果我需要经常传递那些数据并在多个地方修改它。使用数组,你需要通过引用传递它,这会很麻烦,并且可能会引入错误源...对于一个对象,它默认传递为对象引用(不是变量引用,而是指向它的指针)目的)。因此,对于数组,您需要执行以下操作:

function doSomething($inVar, array &$inOutArray) {
    $inOutArray['bar'] = 'baz';
}

For each function that modifies (or might modify) the array.

对于修改(或可能修改)数组的每个函数。

Whereas with an object, you could just do:

而对于一个对象,你可以这样做:

function doSomething($inVar, $object) {
    $object->bar = 'baz';
}

It's shorter (sure, only one character, but it's one character everywhere you might want to modify the array). It's less prone to bugs, since if you later do $object = new Something(), it won't change the original object (since it's not a variable reference)...

它更短(当然,只有一个字符,但它可能是你想要修改数组的一个字符)。它不容易出错,因为如果你以后做$ object = new Something(),它不会改变原始对象(因为它不是变量引用)...

The only argument is that it's slightly less readable, since the & shows you explicitly that you intend to modify that input variable. But if you understand how objects work in PHP 5, you shouldn't be caught off guard (and hence it's an acceptable tradeoff in my mind)...

唯一的论点是它的可读性稍差,因为&明确表示您打算修改该输入变量。但是如果你理解PHP 5中的对象如何工作,你就不应该措手不及(因此在我看来这是一个可以接受的权衡)......