- variable return null even though when return through a function it has a value
- public function add($id, $name, $price, $qty) {
- if($this->check_basket($id)) {
- $this->add_one($id);
- } else {
- $this->items[$id] = array(
- 'name' => $name,
- 'price' => $price,
- 'qauntity' => $qty
- );
- print_r($this->items[$id]);
- }
- }
- class Basket {
- public $items = array();
- public function __construct()
- {
- if($_SESSION['debug']) { echo "session construct <br/>"; }
- $this->items = $_SESSION['cart'];
- }
- public function check_basket($id)
- {
- if(array_key_exists($id, $this->items)) {
- return true;
- }
- return false;
- }
- public function add($id, $name, $price, $qty) {
- if($this->check_basket($id)) {
- $this->add_one($id);
- } else {
- $this->items[$id] = array(
- 'name' => $name,
- 'price' => $price,
- 'qauntity' => $qty
- );
- print_r($this->items[$id]);
- }
- }
- public function add_one($id){
- $curBasket = $this->items["$id"];
- $split = explode("-",$curBasket);
- $split[2] = 1+$split[2];
- $join = implode("-",$split);
- $this->items["$id"] = $join;
- }
- /*public function remove($id) {
- return unset($this->items[$id]);
- }*/
- public function update($id, $quantity)
- {
- $curBasket = $this->items["$id"];
- $split = explode("-", $curBasket);
- $split[2] = 1+$split[2];
- $join = implode("-", $split);
- $this->items["$id"] = $join;
- }
- public function clear() {
- unset($_SESSION['cart']);
- $this->items = "";
- }
- public function __destruct() {
- $_SESION['cart'] = $this->items;
- }
- }
- public function __destruct() {
- $_SESION['cart'] = $this->items;
- }
- public $items;
- $basket = new Basket;
- $basket->add('1', 'Test', '5.00', '2');
- echo "<pre>n";
- print_r($basket);
- echo "</pre>n";
- Basket Object
- (
- [items] => Array
- (
- [1] => Array
- (
- [name] => Test
- [price] => 5.00
- [qauntity] => 2
- )
- )
- )