Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-14-2010, 05:29 AM   PM User | #1
surreal5335
Regular Coder

 
Join Date: May 2008
Posts: 446
Thanks: 23
Thanked 5 Times in 5 Posts
surreal5335 is an unknown quantity at this point
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

Quote:
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:

Quote:
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
surreal5335 is offline   Reply With Quote
Old 12-14-2010, 05:45 AM   PM User | #2
poyzn
Regular Coder

 
poyzn's Avatar
 
Join Date: Nov 2010
Posts: 265
Thanks: 2
Thanked 61 Times in 61 Posts
poyzn is on a distinguished road
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
__________________
Ushousebuilders.com

Last edited by poyzn; 12-14-2010 at 05:59 AM..
poyzn is offline   Reply With Quote
Old 12-14-2010, 06:53 AM   PM User | #3
surreal5335
Regular Coder

 
Join Date: May 2008
Posts: 446
Thanks: 23
Thanked 5 Times in 5 Posts
surreal5335 is an unknown quantity at this point
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.
surreal5335 is offline   Reply With Quote
Old 12-14-2010, 01:41 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 12-14-2010, 08:18 PM   PM User | #5
surreal5335
Regular Coder

 
Join Date: May 2008
Posts: 446
Thanks: 23
Thanked 5 Times in 5 Posts
surreal5335 is an unknown quantity at this point
Here is the output from the var_dump() and print_r()

Quote:
object(shoppingCart)#1 (1) { ["items: protected"]=> array(0) { } }
Array ( )
Thanks a lot
surreal5335 is offline   Reply With Quote
Old 12-14-2010, 10:23 PM   PM User | #6
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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?
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 12-15-2010, 03:39 AM   PM User | #7
surreal5335
Regular Coder

 
Join Date: May 2008
Posts: 446
Thanks: 23
Thanked 5 Times in 5 Posts
surreal5335 is an unknown quantity at this point
The blocks are both in the same file and the var_dump() produces the same results as before.
surreal5335 is offline   Reply With Quote
Old 12-15-2010, 04:25 AM   PM User | #8
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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).
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php

Last edited by Fou-Lu; 12-15-2010 at 04:36 AM..
Fou-Lu is offline   Reply With Quote
Old 12-15-2010, 08:19 AM   PM User | #9
surreal5335
Regular Coder

 
Join Date: May 2008
Posts: 446
Thanks: 23
Thanked 5 Times in 5 Posts
surreal5335 is an unknown quantity at this point
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.
surreal5335 is offline   Reply With Quote
Old 12-15-2010, 09:28 AM   PM User | #10
poyzn
Regular Coder

 
poyzn's Avatar
 
Join Date: Nov 2010
Posts: 265
Thanks: 2
Thanked 61 Times in 61 Posts
poyzn is on a distinguished road
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 
__________________
Ushousebuilders.com

Last edited by poyzn; 12-15-2010 at 10:04 AM..
poyzn is offline   Reply With Quote
Users who have thanked poyzn for this post:
surreal5335 (12-17-2010)
Old 12-17-2010, 06:26 AM   PM User | #11
surreal5335
Regular Coder

 
Join Date: May 2008
Posts: 446
Thanks: 23
Thanked 5 Times in 5 Posts
surreal5335 is an unknown quantity at this point
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
surreal5335 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:50 PM.


Advertisement
Log in to turn off these ads.