RuleBritannia Posted January 16, 2014 Share Posted January 16, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/285424-accessing-object-map-holding-updated-data/ Share on other sites More sharing options...
Skewled Posted January 16, 2014 Share Posted January 16, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/285424-accessing-object-map-holding-updated-data/#findComment-1465486 Share on other sites More sharing options...
requinix Posted January 16, 2014 Share Posted January 16, 2014 (edited) 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 January 16, 2014 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/285424-accessing-object-map-holding-updated-data/#findComment-1465496 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.