我应该将对象或普通数据传递到视图中吗?

时间:2022-02-24 18:08:18

I'm building a timeline similar to Facebook's...with different types of posts, all sorts of data involved with each, but still highly templatized.

我正在建立一个类似于Facebook的时间表...有不同类型的帖子,每种都涉及各种各样的数据,但仍然高度模板化。

I am wondering if it's better to pass large multidimensional arrays into each sub-view (each item on the timeline), or if it's better to simply pass objects in (such as a "user" object), so that the sub view can use the functions of the object?

我想知道是否最好将大型多维数组传递到每个子视图(时间轴上的每个项目),或者如果最好简单地传递对象(例如“用户”对象),以便子视图可以使用对象的功能?

Is one more flexible than the other, and does anybody know how Facebook handles this?

是否比另一个更灵活,有人知道Facebook如何处理这个问题?

2 个解决方案

#1


1  

I can't tell you how facebook handles this, but normally objects are generally more flexible.

我无法告诉你facebook如何处理这个,但通常对象通常更灵活。

You can just echo them by using the __toString function for example. If all objects that get passed into view are automatically decorated, this can be extremely flexible.

例如,您可以使用__toString函数回显它们。如果所有传递到视图中的对象都自动修饰,则可以非常灵活。

They can behave like arrays, if you use the ArrayAccess interface.

如果使用ArrayAccess接口,它们的行为类似于数组。

Generally: Objects are just more dynamical than arrays which are static, so objects are more flexible.

通常:对象比静态的数组更具动态性,因此对象更灵活。

#2


3  

In my opinion arrays are bad idea. Many frameworks passes objects to views, but there are also ones which puts into view arrays (cakePHP for example).

在我看来阵列是个坏主意。许多框架将对象传递给视图,但也有一些框架放入视图数组(例如cakePHP)。

I worked with cake and working with "arrayed" data in views was nightmare

我和蛋糕一起工作,在视图中处理“阵列”数据是噩梦

$data['blah']['bleh']['bluh']['puh']['some_other_long_array_key_for_difference']['and']['so']['on']['fuuuuuu']

Use objects, they are for simplify our lifes ;]

使用对象,它们是为了简化我们的生活;]

#1


1  

I can't tell you how facebook handles this, but normally objects are generally more flexible.

我无法告诉你facebook如何处理这个,但通常对象通常更灵活。

You can just echo them by using the __toString function for example. If all objects that get passed into view are automatically decorated, this can be extremely flexible.

例如,您可以使用__toString函数回显它们。如果所有传递到视图中的对象都自动修饰,则可以非常灵活。

They can behave like arrays, if you use the ArrayAccess interface.

如果使用ArrayAccess接口,它们的行为类似于数组。

Generally: Objects are just more dynamical than arrays which are static, so objects are more flexible.

通常:对象比静态的数组更具动态性,因此对象更灵活。

#2


3  

In my opinion arrays are bad idea. Many frameworks passes objects to views, but there are also ones which puts into view arrays (cakePHP for example).

在我看来阵列是个坏主意。许多框架将对象传递给视图,但也有一些框架放入视图数组(例如cakePHP)。

I worked with cake and working with "arrayed" data in views was nightmare

我和蛋糕一起工作,在视图中处理“阵列”数据是噩梦

$data['blah']['bleh']['bluh']['puh']['some_other_long_array_key_for_difference']['and']['so']['on']['fuuuuuu']

Use objects, they are for simplify our lifes ;]

使用对象,它们是为了简化我们的生活;]