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

发布于 2024-12-11 17:48:14 字数 194 浏览 0 评论 0原文

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

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

其中一个比另一个更灵活吗?有人知道 Facebook 如何处理这个问题吗?

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

树深时见影 2024-12-18 17:48:14

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

我使用蛋糕并在视图中使用“数组”数据是噩梦

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

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

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).

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 ;]

够钟 2024-12-18 17:48:14

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

您可以使用 __toString 函数来回显它们

如果您使用 ArrayAccess 接口。

一般来说:对象比静态数组更具动态性,因此对象更灵活。

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

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.

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文