简体   繁体   English

在PHP中重用对象

[英]Reuse object in PHP

I have a class which includes mysql link and others. 我有一堂课,其中包括mysql链接和其他。 I create an object of this class when I load the front page, but I don't want to create again when I reload the page. 我在加载首页时创建了此类的对象,但是在重新加载页面时不想再次创建。

mysql cannot be stored in session, so is there any other way to keep the object globally? mysql无法存储在会话中,因此还有其他方法可以全局保存对象吗?

I think what you want is persistent database connections . 我认为您想要的是持久数据库连接

This requires slightly reworking your connection code, but then you don't manually manage the persistence (ie you don't manually put something in the session). 这需要稍微修改您的连接代码,但随后您将不会手动管理持久性(即,您无需手动在会话中添加内容)。

Make sure you read up plenty before moving forward, though. 不过,在继续前进之前,请确保已阅读了大量内容。 There are a lot of implications. 有很多含义。

Some options: 一些选项:

  • cache the object using the APC cache 使用APC缓存来缓存对象
  • serialize() the object and save it to a file. serialize()对象,然后将其保存到文件中。 When you need it again, read the file and call unserialize() 当您再次需要它时,读取文件并调用unserialize()

Keep in mind that you won't be able to store resources this way. 请记住,您将无法以这种方式存储资源。 You will want to use persistent connections, as others have mentioned, for that part of your object. 您将要像对象中的其他部分一样使用持久性连接。

Edit I thought I would expound on this a bit, since there's an obvious issue with not being able to save the mysql connection and it is getting comments. 编辑我以为我会对此进行详细说明,因为存在一个明显的问题,即无法保存mysql连接,并且正在获取注释。

If you want to save/restore on object that includes a database connection, you need to deal with the bits that can't be serialized. 如果要在包含数据库连接的对象上保存/恢复,则需要处理无法序列化的位。 You do this by unsetting the attributes when the object is serialized, and then re-creating them when the object is unserialized. 通过在序列化对象时取消设置属性,然后在未序列化对象时重新创建属性,可以实现此目的。 PHP provides two convenient hooks, __sleep() and __wakeup() , for exactly this purpose. PHP为此提供了两个方便的钩子__sleep()__wakeup() You could use them like this: 您可以这样使用它们:

class SaveableConnection {

    $connection = null;

    public function __construct() {
        $this->connect();
    }

    public function connect() {
        // read config and connect
        $this->connection = new PDO($dso);
    }

    public function __sleep() {
        // get rid of the bit that cant be saved
        unset($this->connection);
    }

    public function __wakeup() {
        // re-create the bit that wasn't save on disk
        $this->connect();
    }
}

If you are using persistent database connections, then the value of $this->connection will be optimized for you by copying the connection from the relevant cache. 如果您使用的是持久数据库连接,那么将通过从相关缓存中复制连接来为您优化$this->connection的值。

You can pull this same trick for any object use that implies serialization: saving to files, caching in memcached or APC, and sessions . 您可以对暗示序列化的任何对象使用相同的技巧:保存到文件,在memcached或APC中进行缓存以及会话

If you are tryin got avoid several connectionsto MySQL you can use a persisten connection with mysql_pconnect . 如果您尝试避免与MySQL建立多个连接,则可以对mysql_pconnect使用持久连接。

"First, when connecting, the function would first try to find a (persistent) link that's already open with the same host, username and password. If one is found, an identifier for it will be returned instead of opening a new connection." “首先,在连接时,该函数将首先尝试查找已经使用相同的主机,用户名和密码打开的(持久)链接。如果找到一个链接,则将返回其标识符,而不是打开新的连接。”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM