Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    May 2008
    Posts
    446
    Thanks
    23
    Thanked 5 Times in 5 Posts

    function in class is undefined: OOP help needed

    I am building shopping cart class for my website and currently my addItems() function is undefined. Here is the code the function:

    Code:
    class shoppingCart
    {
    	protected $items = array();
    
    	// adds $product_id to $items array()
    	public function AddItems($product_id)
    	{
    		if(array_key_exists($product_id, $this->items))
    		{
    			$this->items[$product_id] = $this->items[$products_id] + 1;
    		}
    		else 
    		{
    			$this->items[$product_id] = 1;
    		}
    	}
    }
    Here is the code that is calling it:
    Code:
    // get_shopping_cart() is found in functions.php
    // get_shopping_cart() creates a new shoppingCart() object
    $shopping_cart = get_shopping_cart();
    
    if(product_exists($product_id, $layout))
    {
    	$shopping_cart->AddItems($product_id);
    	//echo 'product exits';
    }
    Here is the link to the page displaying the error I am getting:
    http://paperlesswasp.com/millionaire...d=01/marketing


    Thanks a lot for the help

  • #2
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Code:
    $shopping_cart = new get_shopping_cart();

  • #3
    Regular Coder
    Join Date
    May 2008
    Posts
    446
    Thanks
    23
    Thanked 5 Times in 5 Posts
    Sorry about that, here is the code for get_shopping_cart();

    Code:
    // returns a shoppingCart() object
    // function called in add_to_cart.php
    function get_shopping_cart()
    {
    	// $_SESSION is a means for working with cookies 
    	// shopping carts hold temporary data with cookies
    	if(!isset($_SESSION['cart']))
    	{
    		// creates new shoppingCart() object from classes/shoppingCart.php
    		return new shoppingCart();
    	}
    	else 
    	{
    		// unserialize essentially "unzips" an object from memory for use
    		return unserialize($_SESSION['cart']);
    	}
    }

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Is the shoppingCart class file included within the functions.php file? If not, its likely reconstruction an (sorry its been awhile here so I'm kinda fudging this one) PHP_INCOMPLETE_CLASS. I believe this actually shows as its name, so you could confirm if this is the case here:
    PHP Code:
    $shopping_cart get_shopping_cart();
    var_dump($shopping_cart); 
    That must show that its an object of type ShoppingCart. PHP does not allow you to cast, so if its failing its likely because it considers it incomplete and rolls it down to a stdclass object (which won't include the methods).
    Check that out, if thats the case simply include the shoppingcart definition file where the unserialize for the session data is occurring.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #5
    Regular Coder
    Join Date
    May 2008
    Posts
    446
    Thanks
    23
    Thanked 5 Times in 5 Posts
    Ok, I figured it out... The class file was already included and var_dump() confirmed that $shopping_cart was infact an object of shoppingCart class.

    It had to do with the AddItems() function in the class... stupid typo.

    One of my $product_id vars was typed out as $products_id... got rid of the 's' and all is good.

    Thanks for the help


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •