Ik zit met het volgende probleem:
Ik maak een instantie aan van een klasse Cart
$cart = new Cart
Deze wil ik in een session variabele zetten zodat ik die instantie in andere php bestanden ook kan gebruiken
hieronder volgt code:
class.php
<?php
session_start();
include "./klasse.php";
session_register ("cart");
$cart = new Cart;
$cart->add_item("10", 8);
$cart->add_item("9999", 3);
$cart->add_item("0815", 2);
$cart->remove_item("0815",2);
?>
<a href="class2.php">click here</a>
class2.php
<?php
session_start();
include "./klasse.php";
if ($cart)
{
$cart->list_array();
}
?>
klasse.php
<?php
class Cart
{
var $items; // Items in our shopping cart
// Add $num articles of $artnr to the cart
function add_item ($artnr, $num)
{
$this->items[$artnr] += $num;
}
// Take $num articles of $artnr out of the cart
function remove_item ($artnr, $num)
{
if ($this->items[$artnr] > $num)
{
if ($num == 0)
{
//uit de lijst verwijderen
}
else
{
$this->items[$artnr] -= $num;
return true;
}
}
else
{
if ($this->items[$artnr] <= $num)
{
//uit de lijst verwijderen
}
else
{
return false;
}
}
}
//schrijf de waarden uit de array naar het scherm
function list_array () {
reset($this->items); //array pointer naar eerste item
while (list ($key, $value) = each ($this->items))
{
$str .= "<b>$key:</b> $value<br>\n";
}
echo $str;
}
}
?>
Het lukt me niet om de instantie door te geven via sessions.
Kan dit wel en zoja hoe? Zoniet, wat is een andere oplossing?
Ik maak een instantie aan van een klasse Cart
$cart = new Cart
Deze wil ik in een session variabele zetten zodat ik die instantie in andere php bestanden ook kan gebruiken
hieronder volgt code:
class.php
<?php
session_start();
include "./klasse.php";
session_register ("cart");
$cart = new Cart;
$cart->add_item("10", 8);
$cart->add_item("9999", 3);
$cart->add_item("0815", 2);
$cart->remove_item("0815",2);
?>
<a href="class2.php">click here</a>
class2.php
<?php
session_start();
include "./klasse.php";
if ($cart)
{
$cart->list_array();
}
?>
klasse.php
<?php
class Cart
{
var $items; // Items in our shopping cart
// Add $num articles of $artnr to the cart
function add_item ($artnr, $num)
{
$this->items[$artnr] += $num;
}
// Take $num articles of $artnr out of the cart
function remove_item ($artnr, $num)
{
if ($this->items[$artnr] > $num)
{
if ($num == 0)
{
//uit de lijst verwijderen
}
else
{
$this->items[$artnr] -= $num;
return true;
}
}
else
{
if ($this->items[$artnr] <= $num)
{
//uit de lijst verwijderen
}
else
{
return false;
}
}
}
//schrijf de waarden uit de array naar het scherm
function list_array () {
reset($this->items); //array pointer naar eerste item
while (list ($key, $value) = each ($this->items))
{
$str .= "<b>$key:</b> $value<br>\n";
}
echo $str;
}
}
?>
Het lukt me niet om de instantie door te geven via sessions.
Kan dit wel en zoja hoe? Zoniet, wat is een andere oplossing?