du155305 2011-12-14 20:51
浏览 34
已采纳

每个客户端请求都是该类的新实例?

I wrote a class that builds some url in a page every time there is a client request to the server. In a hypothetical scenario in which 100 clients, at the same time, require a server connection, it should produce 100 instances of that class, right? So I would like to know what would be the result of many instances at the same time on the page and if this is a good practice to solve my problem with the "url generator". Thanks

[EDIT] What I tried to do it was to use __set() method and overloading to build the URLs. The fact is that I started to study object-oriented programming in php and I wanted to try a practical application of this method. Here is a piece of code:

 Class BuildPath { 

    private $ServerPath;
    private $ServerUrl;
    private $UrlPath;
    private $data;

    function __construct()
    {
        $this->ServerPath = $_SERVER['DOCUMENT_ROOT'];
        $this->ServerUrl =  $_SERVER['HTTP_HOST'];
        $this->UrlPath = $_SERVER['REQUEST_URI'];
        $this->data = array();
    }

    public function __get($key) {
            return $this->$key;
    }

    public function __set($key,$value)
    {
        $this->data[$key] = $value;
    }

    // others methods
  • 写回答

2条回答 默认 最新

  • dosin84644 2011-12-14 22:11
    关注

    After reading some of the comments I think you are misunderstanding what PHP is and isn't. Each time a user makes a request to the page it is "creating an instance" as you say. After the page is loaded the "instance" is removed. This is how PHP works.

    Singleton classes will not share data with one users instance with another users instance but instead share that users intsance of that class with that same users instance of another class.

    This is how PHP operates and is normal. Since php doesn't "store" data between each request this is where memcache or mysql come into use or sessions.

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

报告相同问题?

悬赏问题

  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面