Jump to content

Recommended Posts

If we have a class which loads a row from the database and can update it etc,

+-------------------------------+
|  id  car  colour  price  fuel |
+-------------------------------+
|  2  bmw   blue   22000 petrol |
+-------------------------------+

If within this class, We run a update to change colour from blue to green, We can maintain this green setting without needing to requery the database, as we can change our internal value of $colour within our class to reflect the change.

 

However, lets say another php script simultaneously does some work on this table, and it changes price from 22000, to 20000

In our other object, we think we have up to date variables, But unknowingly to us we do not.

We can requery the table with ID each time to change all internal variables, But this isnt efficient.

 

Is it possible to link all changes to a central object, and if another script access or updates etc, they do it within this object, that way the change will be reconised anywhere.

 

Thanks in advance.

You could do this by serializing an object and unserializing it later to update it or to allow it to be saved long term in a database. Once you are ok with updating the database you could either store the object or just the changes you want.

PHP isn't really suited to the idea of having a centralized bit of memory where you can stuff objects. Scalar values, like strings and numbers, are easier, but with objects it's a bit more complicated.

 

Anyway, what's the harm of letting your code continue thinking the price is 22000? That was totally okay just seconds ago. So what if the change doesn't take effect immediately? Remember that most scripts take a fraction of a second to run, so it's not like you're holding the object in memory for an extended period.

 

With that said, the #1 time changes like that do matter are when you're editing the object in multiple places (as opposed to editing in just one and viewing in all the others). Don't want one person's changes to be lost, of course. However you can solve that with

a) Modification timestamps, which will tell you if the object changed since you loaded it,

b) Doing an UPDATE with the condition that the current values match what you have in memory, like

UPDATE table SET field1 = "new value 1", field2 = "new value 2" WHERE id = 123 AND field1 = "old value 1" AND field2 = "old value 2"
(after which you check to see if it updated anything)

c) Versioning, which can greatly complicate the system but has distinct advantages you might want to make use of anyways

Edited by requinix
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.