douyun3799 2015-03-21 19:22
浏览 22
已采纳

如何在PHP(Laravel 5)中为我的站点的所有传入连接保留单个实例类?

I am working on a Laravel 5 based web application, I have a class "Connection" that is implemented as a Singleton

<?php

class Connection{

private static $instance = NULL;
private static $connection = NULL;

private function __construct(){
    self::connection=API::openConnection(); //just an example of connection (from other class) 
}

public function getInstance(){


    if(NULL==self::$instance)
    {
        self::$instance=new Connection;
    }  

    return self::$instance;

}

Now the problem I am facing is that, Whenever a connection fetches the instance and instantiates it for first time and opens the connection, but when other user visits the same page (earlier user visited), a connection is reopened(i.e. the class is instantiated again), The openConnection() can give only one connection at a time and the previous one closes when new user opens connection.

Is there any solution that I can use the single connection for multiple user requests?

Note:- Above code is just an abstraction of real problem, to get an idea.

Thanks for any help.

  • 写回答

2条回答 默认 最新

  • dqjmq28248 2015-03-21 19:40
    关注

    If I'm reading your question correctly, you're asking if it's possible to have a connection open on one request, and then have other users visiting the page use that connection without re-opening it.

    The answer to your question, on a purely PHP level, is no. PHP was designed to be a stateless system. Each request that comes into the system forces the program to build up its state, and then that state is lost at the end of every connection. This was in alignment with system design styles at the time, which were all aping HTTP. The pattern you're describing is more common in a desktop or server computing programming environment, where an application stays resident in memory.

    What you will find in PHP is third party libraries that enable the sort of persistent connections you're talking about. For example, the oracle driver (oci8) has a connect function , and a pconnect function. The later implements a persistent connection. These libraries can do this because they're implemented in C/C++ -- i.e., not pure PHP. Also, each library will have its own rules for what "persistent" means.

    When you implement a singleton in PHP, what you're getting is an object that will always be that singleton object for that request only. It's a similar concept to a singleton in java, but because of PHP's vastly different run-time model, you end up using singletons very differently in PHP than you would in systems like java.

    Hope that helps, and good luck!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 对于知识的学以致用的解释
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败