Object Undefinded index

From: Steve Turner (brightspectrum_at_hotmail.com)
Date: 10/30/03


To: <php-general@lists.php.net>
Date: Wed, 29 Oct 2003 19:51:03 -0700

I am just learning to use objects. I am messing around with a shopping cart
object, but am having confusing problems. I am getting an undefined index
error when trying to assign a key value pair to an array variable within an
object. I am confused becuase I believe this is how you assign array
variables outside an object. It acts like the variable is undefined. Thanks
for the help.

Steve Turner

//--------------------------------------------------
class Cart
{
    var $items; // Items in shopping cart

    // Add $quantity articles of $product_id to the cart

    function add_item ($product_id, $quantity = 1)
    {
        $this->items[$product_id] += $quantity;
    }

    // Take $quantity articles of $product_id out of the cart

    function remove_item ($product_id, $quantity)
    {
        if ($this->items[$product_id] > $quantity)
  {
            $this->items[$product_id] -= $quantity;
            return true;
        }
  else
  {
            return false;
        }
    }
}

//I am interacting with the object with the following code
$cart = new Cart;
$cart->add_item("10", 1);

foreach ($cart as $product_id => $quantity)
{
echo $product_id . "<br>" . $quantity;
}



Relevant Pages

  • Re: [PHP] Object Undefinded index
    ... Then your Cart class should be defined as follows: ... Steve Turner wrote: ... but am having confusing problems. ... I am getting an undefined index ...
    (php.general)
  • Re: Tracking Shopping Cart Inventory
    ... Basically, when someone adds an item to their cart, it should alter ... You should have several inventory ... item on hand minus pending orders minus items in the shopping cart ... careful about the inventory cost of merchandise reserved by long-gone ...
    (comp.lang.php)
  • Re: OT Re: Handbags
    ... My grandson, even at 3.5 years, doesn't go into a shopping cart without Mom inserting the cart cover. ... Mom also wipes the handle and seat with the antibacterial wipes provided by the store before the cart cover even gets put on. ... I set my purse in the toddler seat of the shopping cart and weave the cart's belt through the purse's straps. ...
    (rec.crafts.textiles.quilting)
  • for the germ-o-phobes in the group
    ... Shopping Cart Wash Lets Customers Get Groceries, ... The Kirsch brothers want you to know: ...
    (rec.food.cooking)
  • Re: Not sure what to use for this
    ... the shopping cart side is fully functional. ... Then you can still use cookies, etc, because the <iframe> is ... documents that cross domains (security settings of ...
    (comp.lang.java.programmer)