PHP中的对象持久化

发布于 2024-10-03 17:16:16 字数 439 浏览 3 评论 0原文

我对网络编程相当陌生,过去我主要使用java来创建桌面应用程序。

我正在尝试弄清楚如何在 php 中创建持久对象。也许持久不是正确的词,我不希望该对象对于每个客户端都是唯一的,就像我通过在会话变量中序列化它来获得的那样。我希望在服务器上创建该对象,并且始终可以访问同一对象。该对象将查询数据库并存储一些数据。这样,每次页面加载时,php 代码都会从同一个持久对象获取数据,而不必每次都查询数据库。

我目前正在使用单例模式来创建对象,因为我最初的理解是它可以让我完成我想要的事情。对象的一部分是一个数组,当我执行一个向数组添加值的 php 页面,并在同一页面上访问该值时,就可以了。但是,当我向数组添加一个值,然后加载另一个访问该值的页面时,数组又恢复为空。

这可能吗?我是否反应过度,认为查询数据库太多是不好的?有时,任何一秒内都会有多达 20 个用户请求数据,我觉得每次查询数据库都是荒谬的。

谢谢

I am fairly new to web programming, I have mainly used java to create desktop applications in the past.

I'm trying to figure out how to create persistent objects in php. Maybe persistent isn't the right word, I don't want the object to be unique to each client, like i would get by serializing it in a session variable. I want the object to be created on the server and have that same object be accessible at all times. The object would query the database and store some data. This way, each page load, the php code would get that data from the same persistent object rather than having to query the database each time.

I am currently using the singleton pattern for object creation because my initial understanding was that it would allow me to accomplish what i want. Part of the object is an array, and when i execute a php page that adds a value to the array, and access that value on the same page, its fine. However when i add a value to the array and then load another page that accesses the value, the array is back to being empty.

Is this possible? Am i overreacting in thinking that querying the database so much is bad? There will at times be as many as 20 users requesting data during any one second, and i feel like thats ridiculous to query the db each time.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

遥远的绿洲 2024-10-10 17:16:17

PHP 不像 Java 那样有持久化的概念:JVM 允许 Java 应用程序在 HTTP 请求之间持久化在内存中;每次提供新的 HTTP 请求时,网络服务器都会分叉一个新的 PHP 进程,因此对象的静态变量不会在请求之间为您保留数据。

使用数据库来存储持久数据。 Web 编程注重并发性,因此您不必担心数据库查询 - 每秒 20 次已经很少了。如果达到数据库的限制,您可以选择添加缓存层或通过添加只读从属设备来“横向扩展”硬件。

PHP does not have the concept of persistence as Java does: the JVM allows for Java applications to persist in memory between HTTP requests; the webserver forks a new PHP process every time a new HTTP request is served so an object's static variables will not persist data for you between requests.

Use a database to store persistent data. Web programming focuses on concurrency, so you shouldn't worry about database queries - 20 a second is few. If you reach the limits of your database you have the options to add a caching layer or "scale out" hardware by adding read-only slaves.

旧城空念 2024-10-10 17:16:17

通常,您可以通过使用数据库来获得持久性。如果这是一个瓶颈,您可以开始缓存数据,例如在 memcached 中或者本地在您的网络服务器上使用 序列化 数组的文件。

Usually you get your persistence by using the database. If that's a bottleneck you start caching data, for example in memcached or maybe a local file with a serialized array on your webserver.

软甜啾 2024-10-10 17:16:17

虽然这可能不是最漂亮的解决方案,但您可以使用SESSIONS来解决这个问题。

class SomeObject{

    public function __costructor{
        $_SESSION['object'] = serialize($this);
    }

}

在另一页上,您可以简单地调用该对象:

$object = unserialize($_SESSION['object']);

尽管当然这种方法似乎是最简单的。应该采取最大程度的预防措施:

  1. 请注意,取决于服务器流量的会话大小不应太大,因为许多用户同时请求每个会话。您可以自行决定调整大小。

  2. 始终对未完成的会话进行序列化和反序列化,否则会行为不端。

始终 。根据你自己的正念分析来这样做。祝你好运

Though it may not be the prettiest solution, but you can use SESSIONS for this matter.

class SomeObject{

    public function __costructor{
        $_SESSION['object'] = serialize($this);
    }

}

and on another page, you can call the object simply with:

$object = unserialize($_SESSION['object']);

Though ofcourse this approach seems the easiest. It should come with utmost precaution:

  1. Know that sessions depending on the traffic of your server should not be too large in size as many users concurrently ask for each of these sessions. Scale the size at your own discretion.

  2. always serialize and unserialize as sessions not done so will misbehave.

What ever sails your boat. Do so at your own mindful analyzation. goodluck

固执像三岁 2024-10-10 17:16:17

Web 编程中的数据持久性是通过使用 cookie/会话以及跨网页调用传递 cookie/会话变量来完成的。理论上,任何类型的数据都可以存储在这些变量中 - 但对于大多数实际目的,只有识别/重建所需对象(有或没有数据库)所需的更重要的数据(看起来更像令牌)才会被传输到以及来自服务器和浏览器的信息。

Data persistence in Web programming is done thru the use of cookies/sessions and passing cookie/session variables across Web page calls. Theoretically, any kind of data can be stored in these variables - but for most practical purposes, only the more important data (look at them more like tokens) needed to identify/reconstruct the needed objects (with or without a database) are transferred to and from server and browser.

余罪 2024-10-10 17:16:17

我建议您仔细查看 memcached。当您谈论服务器负载和性能能力时,一个有用的指标通常是页数/秒。如果您有专用服务器并且正在处理未优化但非常密集的内容,则您可能每秒只能提供 5 个页面。利用数据缓存是将数据量提高 3 到 10 倍的好方法。然而,就数据的陈旧程度而言,这始终是一个权衡。您确实需要测试您的站点,以正确理解(量化)其他可能的性能限制因素,例如每页的其他连接(图像、CSS 等)、文件 I/O、其他网络活动,以及最后但并非最不重要的实际情况

I'd advise you take a good look at memcached. When you're talking server load and performance capabilities, a useful metric is often pages/second. If you have a dedicated server and unoptimized but very intensive stuff going on, you may only be able to serve 5 pages/second. Utilizing data caching is a great way to increase that 3 to 10 fold. However, it's always a tradeoff as far as how stale the data can get. You will really need to test your site to properly understand (quantify) other possible performance-limiting factors such as other connections per page (images, css, etc), file i/o, other network activity, and last but not least the actual

等待圉鍢 2024-10-10 17:16:17

可以在当前会话中存储对象。现在只需创建一个能够存储和重新创建对象本身的基类。任何派生对象也将是持久的。

您可能想在这里阅读: 持久基类和示例

据我所知会话存储在 RAM 中,因此应该比将对象序列化到磁盘更快以实现持久性。

It is possible to store objects in the current session. Now just create a base class which is able to store and recreate the object itself. Any derived object will then also be persistent.

You might want to read here : persistent base class and example

As far as i know the session is stored in RAM and thus should be faster than serialize the objects to disk to achieve persistence.

陈独秀 2024-10-10 17:16:17

停止使用单例并使用依赖注入。

最好的方法是使用 DataMapper (http://www.martinfowler.com/eaaCatalog/dataMapper.html< /a>) 并通过动态属性将其附加到对象。让数据映射器处理持久性。

$CS = new CookieStorage();
$SS = new SessionStorage();
$FS = new FileStorage('config/object.db');

$DM = new ObjectDataMapper($FS);

$O  = new Object($DM);

$Object->DynamicProperty = 1;

现在,DynamicProperty 将自动保留并自动从文件 object.db 加载。以及类定义:

class Object
{
    public function __construct(MapperInstance $Storage = NULL)
    {
        $this->setMapper($Storage?: new ObjectDataMapper(new FileStorage('config/object.db')));
    }

    public function __get($name)
    {
        $this->_Mapper->getProperty($name);
    }

    public function __isset($name)
    {
        $this->_Mapper->isProperty($name);
    }

    public function __set($name, $value)
    {
        $this->Mapper->setProperty($name, $value);
    }
}

Stop using singleton and use dependency injection.

Best way is to use DataMapper (http://www.martinfowler.com/eaaCatalog/dataMapper.html) and attach it to an object by means of dynamic properties. Let the data mapper handle persistence.

$CS = new CookieStorage();
$SS = new SessionStorage();
$FS = new FileStorage('config/object.db');

$DM = new ObjectDataMapper($FS);

$O  = new Object($DM);

$Object->DynamicProperty = 1;

Now DynamicProperty will automatically persist and will automatically be loaded from file object.db. And the class definition:

class Object
{
    public function __construct(MapperInstance $Storage = NULL)
    {
        $this->setMapper($Storage?: new ObjectDataMapper(new FileStorage('config/object.db')));
    }

    public function __get($name)
    {
        $this->_Mapper->getProperty($name);
    }

    public function __isset($name)
    {
        $this->_Mapper->isProperty($name);
    }

    public function __set($name, $value)
    {
        $this->Mapper->setProperty($name, $value);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文