Grandioso Posted June 22, 2010 Share Posted June 22, 2010 Hi all. I just moved my app (eshop) to another server and the shopping cart stopped working. I create it as an array when the first item is added. It's a 2D array, for each index in the first dimension (different products) there are 4 indexes in the second (product ID, price, etc). On my localhost as on the old server I just used $_SESSION['cart'][$i]['ID'] and there was no problem at all. But on this new server, this array converts into an object. If it wasn't a 2D array, I could use the '->' operator instead of '[]', but here it doesn't always work. I know it, because I checked it with var_dump. On localhost I get array(1) { [0]=> object(stdClass)#1 (4) { ["ID"]=> string(1) "5" ["amount"]=> int(1) ["name"]=> string(25) "Razer ProTone m100, white" ["price"]=> string(5) "19.99" } } , while my new server outputs object(Cart)#4 (0) { } }with the same code ofc). It would be awesome if I knew a way to turn this stupid 'feature' off (note that I don't have direct access to php.ini) Any ideas ? Quote Link to comment https://forums.phpfreaks.com/topic/205561-automatic-array-to-object-conversion/ Share on other sites More sharing options...
Grandioso Posted June 23, 2010 Author Share Posted June 23, 2010 BUMP. Halp meh. Quote Link to comment https://forums.phpfreaks.com/topic/205561-automatic-array-to-object-conversion/#findComment-1076031 Share on other sites More sharing options...
salathe Posted June 23, 2010 Share Posted June 23, 2010 What changed between the old and new servers? Quote Link to comment https://forums.phpfreaks.com/topic/205561-automatic-array-to-object-conversion/#findComment-1076035 Share on other sites More sharing options...
Grandioso Posted June 23, 2010 Author Share Posted June 23, 2010 I don't know. Didn't check the php_info's output, because there are so many things It would take me days to compare That's why I'm trying to find out which setting could be responsible for this. Quote Link to comment https://forums.phpfreaks.com/topic/205561-automatic-array-to-object-conversion/#findComment-1076076 Share on other sites More sharing options...
trq Posted June 23, 2010 Share Posted June 23, 2010 because there are so many things It would take me days to compare Days? Off the top of my head there isn't a setting that would create such an effect. Can we see some code? Quote Link to comment https://forums.phpfreaks.com/topic/205561-automatic-array-to-object-conversion/#findComment-1076079 Share on other sites More sharing options...
Grandioso Posted June 23, 2010 Author Share Posted June 23, 2010 OK, not days, but certainly longer than I could take Here's how the cart variable is initialized. public function addItem($id, $amt, $nm, $prc) { if (!isset($_SESSION['cart'])) $_SESSION['cart'] = array(); if (!$this->inCart($id)) { $i = count($_SESSION['cart']); $_SESSION['cart']->$i->ID = $id; $_SESSION['cart']->$i->amount = $amt; $_SESSION['cart']->$i->name = $nm; $_SESSION['cart']->$i->price = $prc; } } (at first it was assigned through "[]", I changed it because it was throwing errors) When I try to get the values, I either get errors (trough "[]") or NULL (trough "->"). Quote Link to comment https://forums.phpfreaks.com/topic/205561-automatic-array-to-object-conversion/#findComment-1076124 Share on other sites More sharing options...
trq Posted June 24, 2010 Share Posted June 24, 2010 The reason it is being converted to an object is because you are referencing it using ->. Quote Link to comment https://forums.phpfreaks.com/topic/205561-automatic-array-to-object-conversion/#findComment-1076340 Share on other sites More sharing options...
Grandioso Posted June 24, 2010 Author Share Posted June 24, 2010 I wish it was. But it's not. At first I was accessing it trough "[]", but then it threw me errors complaining that I can not access object variables trough []. I only changed it later. And what's most important : the same code on a different server runs just fine. So it must somewhere in the server settings. I just put it back the way it was (with []). It works on localhost but doesn't work on my new server. btw the old server runs an older version of PHP, and my local server runs a newer one as the new server. Quote Link to comment https://forums.phpfreaks.com/topic/205561-automatic-array-to-object-conversion/#findComment-1076537 Share on other sites More sharing options...
JonnoTheDev Posted June 24, 2010 Share Posted June 24, 2010 Type cast the object as an array $x = new object(); $x->name = "Neil"; $x->age = 29; $y = (array)$x; print_r($y); Quote Link to comment https://forums.phpfreaks.com/topic/205561-automatic-array-to-object-conversion/#findComment-1076581 Share on other sites More sharing options...
Grandioso Posted June 26, 2010 Author Share Posted June 26, 2010 Still does'nt work The array is empty after conversion. Quote Link to comment https://forums.phpfreaks.com/topic/205561-automatic-array-to-object-conversion/#findComment-1077587 Share on other sites More sharing options...
Grandioso Posted June 27, 2010 Author Share Posted June 27, 2010 I can hardly believe it, but it's SOLVED ! I created an .htaccess file in the root of the application and put this line in it : php_value register_globals off Now it's working like a charm. So happy Quote Link to comment https://forums.phpfreaks.com/topic/205561-automatic-array-to-object-conversion/#findComment-1077812 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.