Memory resident Object that survive a request?

Posted on 2004-10-27
Last Modified: 2007-12-19
Well this is a bit theoretical: I want to create an object once and use it in multiple requests, this is the short version.

The long version is: I have an object which loads a lot of data (some mega) into an array, now I realise that loading it on each request is a huge waste of time since the data does not change often. Is it possible to build an object which can be reused (without having to re-initialize it) for more than one request?
Question by:Snyke
    LVL 48

    Accepted Solution

    you can use the serialize and unserialze function of php to store that object into a file

    To store the object in the memory might be a littel more complicated. Have a look athe the shared memory extension of php (not available on Windows)
    LVL 2

    Expert Comment

    You can also use sessions(overcomes read/write time), which by default serializes and unserializes data.

    1. Register your class object using session_register(object)
    2. Include your class file ex: in every php page.

    LVL 3

    Expert Comment


    This is one area where PHP differs from application servers like Tomcat, BEA.

    PHP does not have application-wide global objects.

    Sessions IS a way to simlate that, just remember then that this object will be copied once per each session. :-]
    ie. 100 users = 100 sessions = 100 mb of the objects.

    I am looking at this prob for some time and have decided to write some SHM-based global object manager; I will probably even put it somewhere under GNU license, but it is not there yet.
    LVL 1

    Author Comment

    As I see it, the option of storing the stuff in the session is just as good as loading the thing everytime, since sessions are itself stored in files...
    LVL 48

    Expert Comment

    The advantage of having it in an extra file is, that if you update that file all next request will have that updated object. If it's stored in the session, the old old version may last a long time...

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
    Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    884 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now