dougu1990 2013-02-28 15:08
浏览 89
已采纳

我们是否需要持久化对象来实现php中的单例模式

I am having singleton Database class in classes/database.php as mentioned in Singleton pattern in php and I am creating the database object in users.php using Database::getInstance() method then I am calling the same static method in accounts.php (Database::getInstance()), here I am getting new instance instead of the instance that I created in users.php. I am new to design patterns and confused a bit.

my questions are

  1. Do we need to have persistent object to achieve singleton pattern in php?
  2. If not then what are all the ways that we keep the single instance all over the application.
  • 写回答

1条回答 默认 最新

  • dqo58772 2013-02-28 15:17
    关注

    Desktop applications often use a single database connection that persists. However, in PHP and most other web languages/frameworks this should not be the case. Instead of a single long lived connection, you use a short lived connection for each page load. Rather than thinking of your application as a whole, think of the process of each page load. In PHP, no other page load knows about the other unless you persist the data in a database or in $_SESSION or other places.

    The singleton pattern in PHP is one of singleton throughout the page load. Although initially it may seem like "What's the point? Its just a page load", when you get to larger applications, it becomes obvious that each page load is not an insignificant operation. In systems like Drupal, you can have 10's to 100's of database queries happening each page load

    Oh, and the singleton pattern is considered Bad Practice and will bite you later. In fact, it should probably be avoided in all of your applications. A better pattern would be something like the DAO pattern or the Factory pattern. Please, save yourself and don't use singletons (I've made that mistake so many times).

    As for persisting objects between page loads (which is very necessary to almost any application), that is what your database and $_SESSION variable is for. You could also use alternative caching options like APC (http://php.net/manual/en/book.apc.php). Your application should be able to re-construct what it needs to based on whatever it finds in your database. For example, a user is created. You persist that user's data to the database. Whenever someone comes around and wants to know about the user, it pulls it out of the database to re-create your user object. Another example is that a user logs in. You want them to stay logged in, so you persist some kind of variable to $_SESSION that is checked every page load to see if they are logged in and who they are logged in as. You do some checking and then load the user that they say they are logged in as from your database.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记