DAO 对象是否比 PHP 中的静态 DAO 类更好? [英] Are DAO objects better than static DAO classes in PHP?

查看:35
本文介绍了DAO 对象是否比 PHP 中的静态 DAO 类更好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解在 Java 中不使用静态的原因.

I understand the reasons for not using statics in Java.

但是,我目前正在用 PHP 开发 OO 代码.我使用 DAO 的目的是将我的查询放在一个地方,以便我可以轻松找到它们.我还实例化了一些 DAO,这样我就可以在一些(相关)查询中加入分页.在许多情况下,这不是必需的,因此我倾向于只创建以下形式的静态方法(尽管从技术上讲我认为我不能将其称为 DAO):

However, I'm currently developing OO code in PHP. I use DAOs with the goal of keeping my queries in one place so I can easily find them. I also instantiate some DAOs so I can incorporate pagination in some (relevant) queries. In many cases, it's not necessary and so I tend to just create static methods (even though technically I don't think I can call that a DAO) in the form:

$info = schemeDAO::someFunction($variable);

在页面刷新期间我可能只需要那个方法(即头文件中的特定值).

I may need only that single method during a page refresh (i.e. a specific value in a header file).

在创建和销毁对象时,我可能需要将同一个 DAO 实例化一百次.

I may need to instantiate the same DAO a hundred times as objects are created and destroyed.

 $dao = new myDao();
 $info = $dao->someFunction($variable);

无论哪种方式,在我看来,至少在 PHP 中,简单地加载静态数据并将其保存在内存中不是更有效吗?

Either way, it seems to me, in PHP at least, wouldn't it be more performance efficient to simply load a static and keep it in memory?

推荐答案

虽然静态访问是可以接受的(在一定程度上),但使用动态方法您可以通过依赖将对象传递给第三侧对象,(否则也传递调用依赖项的转换必须从原始类启动),不需要推送一些数据,而是依赖项决定并在单个方法中多次拉取它需要的数据/方法.否则它只能返回,而实例可以被调用,未从数据中分离的包装方法逻辑.实例内联代码似乎更短,当你删除一个实例时,他们所有的调用都会在那一刻抱怨,而静态类继续在代码中不被注意,因为它们不需要实例化先决条件.静态类在各种对象和方法上下文之间保存它们的状态,因此不会自动重置".就像新构造"一样.实例鼓励更透明的纯函数方法 - 传递参数.当你传递一个对象时,你并没有将服务逻辑与它的数据结构分开,当你只传递数组数据结构时,执行逻辑在传输中丢失或分离到另一个地方,最终必须在不透明的情况下静态调用 -传递 - 纯函数概念.

While the static access is acceptable (to an extent), with the dynamic approach you can pass the object transitively to the 3rd side object via dependency, (otherwise also the transitive call the transition of dependency would have to be initiated from the original class), which needs not to be pushed some data, but rather the dependency decides and pulls the data/method it needs be it multiple times in a single method. Otherwise way it can only return, while instance can be called, not-separated wrapper method logic from data. Instance inline code seems to be shorter, and when you remove an instance, all their calls complain at that moment, whereas static class continues to preserve unnoticed in the code as they don't need the instantiation prerequisite. Static classes preserve their state in between various objects, and methods contexts and thus are not automatically "reset" as it is with the 'new construct'. Instances encourage more transparent pure functions approach - passing parameters. When you pass an object, you don't separate the service logic from it's data structure, when you pass only the array data structure, the execution logic is lost in transit or separated into another place and must be eventually called intransparently statically when not-passed - pure functions concept.

我会比较爱因斯坦方程和牛顿方程.在某些情况下,它们看起来完全一样.但是为了满足,我会使用更通用的实例或服务定位器单例,而不是静态类.另一方面,不那么通用"的静态类最初可能更容易实现,特别是如果您不打算将它们与火箭一起使用到太空轨道,就像使用实例一样.与私有属性类似,您表示它们不会在任何地方传递,纯函数,尽管它可能不太经常发出从任何地方调用的坏信号.

I would use comparison with Einsteins vs Newton's equations. In some cases, they look totally the same. But to be pretty content I would use more versatile instances or service locator singletons, vs static classes. On the other side, the less "versatile" static classes might be initially easier to implement, especially if you don't plan on rocket with them as far as to the space orbit as you might get with instances. Similarly as with private attributes you signal they are not passed anywhere, pure functions, though it might less often signalize also the bad, being called from anywhere.

这篇关于DAO 对象是否比 PHP 中的静态 DAO 类更好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆