Memory resident Object that survive a request?

Posted on 2004-10-27
Medium Priority
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

hernst42 earned 200 total points
ID: 12419242
you can use the serialize and unserialze function of php to store that object into a file
see http://de3.php.net/manual/en/function.serialize.php

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) http://de3.php.net/manual/en/ref.sem.php

Expert Comment

ID: 12419447
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: YourClass.inc in every php page.


Expert Comment

ID: 12419623

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.

Author Comment

ID: 12422667
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

ID: 12422750
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...

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to implement server side field validation and display customized error messages to the client.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

601 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