Question : class construct problem

Hi experts :)

I've got a class called shoppingcart. There's are problem when creating an instance for the second time. It seems to be an PHP config problem as this code does work on 2 other webservers.

This is the class
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
class ShoppingCart{
	
	public function __construct(){
	// Check and see if shopping cart already exists, if it doesn't, set the array to the variable
		if($_SESSION['cart'] == null){
			$_SESSION['cart'] = array();
			$_SESSION['cart']['totPrice'] = 0;
			
			$_SESSION['cart']['totKorting']['totaal'] = 0;
			$_SESSION['cart']['totKorting'][6] = 0;
			$_SESSION['cart']['totKorting'][19] = 0;
		}
		if($_SESSION['userinfo'] == null){
			$_SESSION['userinfo'] = array();
		}
	}

.........


In my index.php:
$cart =  new ShoppingCart();

When I load index.php for the first time all session variables correctly created. However, when I load/refresh the page, the var $_SESSION['cart'] is set to object ShoppingCart?! What's going on here...

Answer : class construct problem

double check your index.php page has session_start();

try doing a var_dump() on both $cart and $_SESSION['cart']

var_dump($cart);
should return:
---------------------------
object(ShoppingCart)#1 (0) { }


var_dump($_SESSION['cart']);
should return:
-----------------------------
array(2) { ["totPrice"]=>  int(0) ["totKorting"]=>  array(3) { ["totaal"]=>  int(0) [6]=>  int(0) [19]=>  int(0) } }
Random Solutions  
 
programming4us programming4us