...

View Full Version : OOP problem



surreal5335
12-14-2010, 06:29 AM
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:



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:



$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

poyzn
12-14-2010, 06:45 AM
1. If the session is notbeing isset, then $shopping_cart gets array not the object

$shopping_cart = unserialize($_SESSION['cart']);

so you can't use it as an object.

2. put next function into shoppingCart class


function get()
{
if(!isset($_SESSION['cart']))
{
return unserialize($_SESSION['cart']);
}
}

then you may create an object and get cart value into $cart_array variable.


$shopping_cart = new shoppingCart();
$cart_array = $shopping_cart->get();

and work with $cart_array

surreal5335
12-14-2010, 07:53 AM
Thanks for the reply, but actually my code does state to create an object if $_SESSION['cart'] is not set:



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:



if(!isset($_SESSION['cart']))
{
return unserialize($_SESSION['cart']);
}


I believe what you are going after would be:



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.

Fou-Lu
12-14-2010, 02:41 PM
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:


var_dump($shopping_cart);
print_r($_SESSION);

And post the results.

surreal5335
12-14-2010, 09:18 PM
Here is the output from the var_dump() and print_r()



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


Thanks a lot

Fou-Lu
12-14-2010, 11:23 PM
And before this block:


$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?

surreal5335
12-15-2010, 04:39 AM
The blocks are both in the same file and the var_dump() produces the same results as before.

Fou-Lu
12-15-2010, 05:25 AM
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.


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:


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).

surreal5335
12-15-2010, 09:19 AM
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:



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:



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.

poyzn
12-15-2010, 10:28 AM
why are you making it so complicated, try this construction


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

surreal5335
12-17-2010, 07:26 AM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum