1

Maybe it's a stupid question. But anyway here is my problem. I have multiple classes in my project.

At the beginning the constructor of the class Calculate($param1, $param2...) is called.

This Calculate class is called multiple times via jQuery Events (click, change..) depending on which new form field is filled.. The prices and values are calculated in the background by php and are represented on the website via AJAX (live while typing).

The connection between the AJAX and the Calculate class is a single file (jsonDataHanlder) this file receives the POST-values from the AJAX and returns a JSON-String for the website output. So every time I call this jsonDataHandler a new Calculate object is beeing created. With the updated values, but never the first created object. I am experiencing now multiple problems as you may can imagine.

How can I always access the same object, without creating an new one?

EDIT: because of technical reasons, I cannot use sessions..

Jake
  • 175
  • 6
  • 17
  • Are you saying you need to preserve information from the first object instantiated? – WheatBeak Mar 14 '16 at 19:33
  • yes.. the perfect case would be to create an object once and just set the values via the ajax call. – Jake Mar 14 '16 at 19:38
  • Save it in the $_SESSION then. – WheatBeak Mar 14 '16 at 19:39
  • oh, I forgot. I cannot use sessions. Not because I don't know, but because sessions are causing conflicts with our online shop system.. so sessions are not an option :( – Jake Mar 14 '16 at 20:02

2 Answers2

0

Maybe you should try to save the values of the parameters of Calculate object in database, and every you make an AJAX call you take the latest values from the DB.

Tim
  • 9
  • 1
  • That's also not an option. This script generates shop articles. About 1000 / day.. to make always an update on a table would be a mess.. So, is there not a fault in my thinking? I mean is there not a way to create on PHP object and use it all along? – Jake Mar 14 '16 at 20:45
  • Well the only way to keep track of the information while client and server communicate (as far as I know) is to use sessions or cookies or storing the information in DB. – Tim Mar 15 '16 at 07:23
0

Here is the php application lifetime:

  • The browser sends an http request to the web-server
  • Web-server (for example Apache), accepts the request and launches your php application (in this case your jsonDataHandler file)
  • Your php application handles the request and generates the output
  • Your php application terminates
  • Web-server sends the response generated by php application to the browser

So the application "dies" at the end of each request, you can not create an object which will persist between requests.

Possible workarounds:

  • Persist the data on the server - use sessions or the database (as you said this is not an option for you)
  • Persist the data on the client - you still create your object for each request, but you keep additional information client-side to be able to restore the state of your object (see more on this below)
  • Use something like reactphp to have your application running persistently (this also can be not an option because you will need to use different environment). Variance of this option - switch to another technology which doesn't re-launch the server-side application each time (node.js, python+flask, etc).

So, if you can't persist the data on the server, the relatively simple option is to persist the data on the client.

But this will only work if you need to keep the state of your calculator for each individual client (vs keeping the same state for all clients, in this case you do need to persist data on the server).

The flow with client-side state can be this:

  • Client sends the first calculation request, for example param1=10
  • Your scripts responds with value=100
  • Client-side code stores both param1=10 and param1_value=100 into cookies or browser local storage
  • Client sends the next calculation, for example param2=20, this time the client-side code finds previous results and sends everything together (param1=10&param1_value=100&param2=20)
  • On the server you now can re-create the whole sequence of calculation, so you can get the same result as if you would have a persistent Calculate object
Borys Serebrov
  • 15,636
  • 2
  • 38
  • 54
  • Thanks a lot for this very good explaination. I think I will have to go the cookie way.. I'll give it a try. – Jake Mar 16 '16 at 11:25
  • @MarLon do you mean submit to the server? it is not necessary, you can set / get cookies from the js code, check [this](http://stackoverflow.com/questions/4825683/how-do-i-create-and-read-a-value-from-cookie) or [this](http://stackoverflow.com/questions/14573223/set-cookie-and-get-cookie-with-javascript). – Borys Serebrov Mar 16 '16 at 12:23
  • oh, that looks interesting. I thought I need a form submit to get and set new cookie data. – Jake Mar 20 '16 at 19:30