object persistence in php

怎甘沉沦 提交于 2019-11-30 03:18:45
AndreKR

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.

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.

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

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.

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

mAsT3RpEE

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);
    }
}

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!