anointing Posted September 1, 2008 Share Posted September 1, 2008 Hi, I'm from a java background newly entering php. I want to implement a simple cart object which contains an array of items. The items themselves are to be encapsulated in an item object. I'm using PHP Version 5.2.5 with Apache 2.2. The problem is cart.php which is supposed to add an item to a cart object simply does not see both classes. I know something needs to be done to give php access to the classes. Not sure exactly what. Please advice. Both item and cart classes are inthe same root directory ======== Here is the Item class contained in item.php: <?php class Item { var $ref; // Cake reference number var $name; // Cake name var $order; // Cake oder details var $cost; // Cost of order var $qty; // Number of cakes ordered // instantiate object function __construct($ref, $name, $order, $cost, $qty) { $this->ref = $ref; $this->name = $name; $this->order = $order; $this->cost = $cost; $this->qty = $qty; } // get reference function get_ref() { return $this->ref; } // get cake name function get_name() { return $this->name; } // get cake order function get_order() { return $this->order; } // get cake costs function get_cost() { return $this->cost; } // get cake quantity function get_qty() { return $this->qty; } } ?> ======== Here is the Cart class contained in cart.php: <?php session_start(); include("item.php"); $items; function __construct() { $this->items = array(); } function addItem($ref, $name, $order, $cost, $qty) { if(count($this->items)==0) { $anItem= new Item($ref, $name, $order, $cost, $qty); $this->items[$ref] = $anItem; //$_SESSION['cart'] = $this; } else { foreach($this->items[] as $k => $v) { $itemobject= (Item)$this->items[$k]; if($itemobject->ref==$k) { echo "This item has already been selected"; } } } } Link to comment https://forums.phpfreaks.com/topic/122219-problem-with-cart-object/ Share on other sites More sharing options...
knowj Posted September 1, 2008 Share Posted September 1, 2008 you need on your cart class class cart extends item { Link to comment https://forums.phpfreaks.com/topic/122219-problem-with-cart-object/#findComment-631054 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.