object persistence in php

后端 未结 7 1963
孤城傲影
孤城傲影 2020-12-14 02:48

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

相关标签:
7条回答
  • 2020-12-14 02:54

    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

    0 讨论(0)
  • 2020-12-14 03:00

    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.

    0 讨论(0)
  • 2020-12-14 03:02

    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.

    0 讨论(0)
  • 2020-12-14 03:03

    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.

    0 讨论(0)
  • 2020-12-14 03:09

    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

    0 讨论(0)
  • 2020-12-14 03:12

    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.

    0 讨论(0)
提交回复
热议问题