Jump to content

automatic Array to object conversion


Grandioso

Recommended Posts

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 ?

Link to comment
Share on other sites

OK, not days, but certainly longer than I could take :D

 

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 "->").

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.