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.

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

报告相同问题?

悬赏问题

  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?