我们为什么要获取对象?

时间:2021-12-11 06:01:32

I'm not referring to a particular database driver or so but we, as PHP developer, have always had the choice to use either arrays or object as return type of a database query.

我不是指特定的数据库驱动程序,但作为PHP开发人员,我们总是可以选择使用数组或对象作为数据库查询的返回类型。

So for example we could access the column "title" with either $array['title'] or $object->title.

因此,例如,我们可以使用$ array ['title']或$ object-> title访问列“title”。

For example mysql PHP built-in functions mysql_fetch_assoc() and mysql_fetch_object() give this choice.

例如,mysql PHP内置函数mysql_fetch_assoc()和mysql_fetch_object()给出了这个选择。

I noticed that the object-way (as I'm gonna call it) is much worse:

我注意到对象方式(因为我要称它)更糟糕的是:

  • You cannot specify table names that contains special chars such as . or - and use them with objects.
  • 您不能指定包含特殊字符的表名称,例如。或 - 并将它们与对象一起使用。

  • Object are usually heavier than arrays
  • 对象通常比数组重

  • Object doesn't have all the cool functions to manage them as arrays do
  • 对象没有像数组那样管理它们的所有酷函数

  • Arrays better explain the table structure rather then property chaining.
  • 数组更好地解释表结构而不是属性链。

There are few other little things about object vs array war regarding database results but these are the main ones.

对于数据库结果,关于对象与阵列战争的其他一些小事情,但这些是主要的。

Why do people even take the object-way as choice?

为什么人们甚至把对象作为选择?

2 个解决方案

#1


3  

  • You can specify table names that contains special chars such as . or -
  • 您可以指定包含特殊字符的表名称,例如。要么 -

  • Object are more flexible than arrays
  • 对象比数组更灵活

  • Object have all the cool functions that you can imagine to manage them, you just have to write a few of them
  • 对象具有您可以想象的所有很酷的功能来管理它们,您只需要编写一些它们

  • Arrays don't cleanly express relationships between tables, object mapping does.
  • 数组不能干净地表达表之间的关系,对象映射确实如此。

#2


1  

I mostly use it because $user->name is a lot easier on the eyes than $user['name']

我主要使用它,因为$ user-> name在眼睛上要比$ user ['name']容易得多

#1


3  

  • You can specify table names that contains special chars such as . or -
  • 您可以指定包含特殊字符的表名称,例如。要么 -

  • Object are more flexible than arrays
  • 对象比数组更灵活

  • Object have all the cool functions that you can imagine to manage them, you just have to write a few of them
  • 对象具有您可以想象的所有很酷的功能来管理它们,您只需要编写一些它们

  • Arrays don't cleanly express relationships between tables, object mapping does.
  • 数组不能干净地表达表之间的关系,对象映射确实如此。

#2


1  

I mostly use it because $user->name is a lot easier on the eyes than $user['name']

我主要使用它,因为$ user-> name在眼睛上要比$ user ['name']容易得多