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 11 of 11

Thread: OOP problem

  1. #1
    Regular Coder
    Join Date
    May 2008
    Posts
    446
    Thanks
    23
    Thanked 5 Times in 5 Posts

    OOP problem

    Well I have two problems...

    1. my function for creating an object is failing. When I echo out the variable which is suppose to hold the object I get "Array"

    Here is the error I am getting with it

    Fatal error: Call to a member function AddItems() on a non-object in /home/paper754/public_html/millionaire/add_to_cart.php on line 29
    Here is my code I am using:

    PHP Code:
    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']);
        }
    }

    $shopping_cart get_shopping_cart(); 
    The problem gets noticed here:

    PHP Code:
    $product_id $_REQUEST['id'];

    if(
    product_exists($product_id$layout))
    {    

        
    $shopping_cart->AddItems($product_id);



    2. My other, after trying to determine if its my function call thats the problem I by pass it and just create an object right there $shopping_cart = new shoppingCart();

    This is what I got from it:

    Catchable fatal error: Object of class shoppingCart could not be converted to string in /home/paper754/public_html/millionaire/add_to_cart.php on line 28
    Thanks for the help

  • #2
    Regular Coder poyzn's Avatar
    Join Date
    Nov 2010
    Posts
    266
    Thanks
    2
    Thanked 61 Times in 61 Posts
    1. If the session is notbeing isset, then $shopping_cart gets array not the object
    PHP Code:
     $shopping_cart =  unserialize($_SESSION['cart']); 
    so you can't use it as an object.

    2. put next function into shoppingCart class
    PHP Code:
    function get()
    {
       if(!isset(
    $_SESSION['cart']))
       {
             return 
    unserialize($_SESSION['cart']);
       }

    then you may create an object and get cart value into $cart_array variable.
    PHP Code:
    $shopping_cart = new shoppingCart();
    $cart_array $shopping_cart->get(); 
    and work with $cart_array
    Last edited by poyzn; 12-14-2010 at 06:59 AM.

  • #3
    Regular Coder
    Join Date
    May 2008
    Posts
    446
    Thanks
    23
    Thanked 5 Times in 5 Posts
    Thanks for the reply, but actually my code does state to create an object if $_SESSION['cart'] is not set:

    PHP Code:
    if(!isset($_SESSION['cart']))
        {
            
    // creates new shoppingCart() object from classes/shoppingCart.php
            
    return new shoppingCart();
        } 
    also I believe that returning an unserialized session that hasnt been set yet would not work:

    PHP Code:
       if(!isset($_SESSION['cart']))
       {
             return 
    unserialize($_SESSION['cart']);
       } 
    I believe what you are going after would be:

    PHP Code:
       if(isset($_SESSION['cart']))
       {
             return 
    unserialize($_SESSION['cart']);
       } 
    probably just typos, I make them all the time and sadly spend hours trying to debug a problem that is as simple as taking out or adding 1 character in my code.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    PHP Code:
    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']);
        }
    }

    $shopping_cart get_shopping_cart(); 
    Following the $shopping_cart please add:
    PHP Code:
    var_dump($shopping_cart);
    print_r($_SESSION); 
    And post the results.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #5
    Regular Coder
    Join Date
    May 2008
    Posts
    446
    Thanks
    23
    Thanked 5 Times in 5 Posts
    Here is the output from the var_dump() and print_r()

    object(shoppingCart)#1 (1) { ["items: protected"]=> array(0) { } }
    Array ( )
    Thanks a lot

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    And before this block:
    PHP Code:
    $shopping_cart->AddItems($product_id); 
    Also add a var_dump($shopping_cart). Does it show the same object in use?

    These two blocks, are they in the same script or different scripts?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #7
    Regular Coder
    Join Date
    May 2008
    Posts
    446
    Thanks
    23
    Thanked 5 Times in 5 Posts
    The blocks are both in the same file and the var_dump() produces the same results as before.

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Can you post the class definition for shoppingCart?
    If it clearly shows its of type shoppingCart (and not of stdclass or incomplete class) than the only explaination is that the shoppingCart class does not have an AddItems method in it.

    Edit:
    My bad, I thought it was an error on finding an undefined method, not a method on an undefined object.
    Can you confirm that your line numbers are correct? If this:
    PHP Code:
    var_dump($shopping_cart);
    $shopping_cart->AddItems($product_id); 
    Shows this dump as a valid shoppingCart object then it should not be puking on the next line as a non-object. Also, forgot to mention a few posts back there that your session is empty, so you will want to make sure you test it with a session too. Objects should serialize themselves as well, so you shouldn't need to actually call a serialize/unserialize on an object from a session. If the class includes resources like a database connection, you must override the __sleep and __wakeup methods to close and open resources (serialized resources sometimes work and sometimes don't, so its better to never serialize one).
    Last edited by Fou-Lu; 12-15-2010 at 05:36 AM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #9
    Regular Coder
    Join Date
    May 2008
    Posts
    446
    Thanks
    23
    Thanked 5 Times in 5 Posts
    Ok so I looked at my code and by having a var_dump() just before the AddItems() method call, I got this:

    array(1) { [0]=> string(12) "01/marketing" }

    bypassing the function I get this:

    object(shoppingCart)#1 (1) { ["items: protected"]=> array(0) { } }

    So clearly my problem is my get_shopping_cart() function and I will bet the $_SESSION is the culprit.

    Based on my get_shopping_cart() function:

    PHP Code:
    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']);
        }

    this would mean my $_SESSION is being prematurely set, thus skipping the instructions to create a new object and go to unserialize the session.

    I have a set_shopping_cart() function which is found at the bottom of the page calling all these functions:

    PHP Code:
    function set_shopping_cart($cart)
    {
        
    $_SESSION['cart'] = serialize($cart);

    This is the only place I know of that would set a session. I commented it out to see what I would get and no change was made.

  • #10
    Regular Coder poyzn's Avatar
    Join Date
    Nov 2010
    Posts
    266
    Thanks
    2
    Thanked 61 Times in 61 Posts
    why are you making it so complicated, try this construction
    PHP Code:
    class ShoppingCart()
    {
       private 
    $items;
       function 
    __construct()
       {
            
    $this->items unserialize($_SESSION['cart']); // get items from session on object init
       
    }

       function 
    get_shopping_cart()
       {
            return 
    $this->items;
       }
       
       function 
    AddItem()
       {
           
    // here and afted go your functions
           // get items by $this->items;
       
    }

    }


    $shopping_cart = new shoppingCart(); 
    // after the initialization your object already includes unserialized session cart value
    $items $shopping_cart->get_shopping_cart(); 
    // if you want to work with items outside the object - just get them 
    Last edited by poyzn; 12-15-2010 at 11:04 AM.

  • Users who have thanked poyzn for this post:

    surreal5335 (12-17-2010)

  • #11
    Regular Coder
    Join Date
    May 2008
    Posts
    446
    Thanks
    23
    Thanked 5 Times in 5 Posts
    Thanks a lot, that finally did it... I may be making this session topic harder than it is, but right now I am pretty hazy on how its working. Does anyone know of any good video tutorials that explain how sessions work? When a topic is confusing already, I find reading about it will only make it worse (Learned that from Java swing).

    Thanks again fro all your 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
    •