__PHP_Incomplete_Class_Name

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mordiaii2
Forum Newbie
Posts: 1
Joined: Fri Aug 28, 2009 8:09 pm

__PHP_Incomplete_Class_Name

Post by mordiaii2 »

I'm writing a shopping cart in PHP, and I have run into an issue with my item object.
Add.php

Code: Select all

<?
    include("Item.php");
    $temp = new Item($_POST['sku'], $_POST['name'], $_POST['price'], $_POST['amount']);
    session_start();
    $count = count($_SESSION['cart']) + 1;
    
    $_SESSION['cart'][$count] = $temp;
    print_r($_SESSION['cart'][$count]);
    
?>
Cart.php

Code: Select all

<?
    session_start();
    include("Item.php");
    $_SESSION['cart'][1] = new Item(1, "Test", 10.00, 5);
    $_SESSION['cart'][2] = new Item(2, "Test", 10.00, 5);
    $_SESSION['cart'][3] = new Item(3, "Test", 10.00, 5);
    $_SESSION['cart'][4] = new Item(4, "Test", 10.00, 5);
    $_SESSION['cart'][5] = new Item(5, "Test", 10.00, 5);
    $_SESSION['cart'][6] = new Item(6, "Test", 10.00, 5);
    $_SESSION['cart'][7] = new Item(7, "Test", 10.00, 5);
    $_SESSION['cart'][8] = new Item(8, "Test", 10.00, 5);
    $_SESSION['cart'][9] = new Item(9, "Test", 10.00, 5);
    $_SESSION['cart'][10] = new Item(10, "Test", 10.00, 5);
    print_r($_SESSION);
    
    $count = 1;
    $total = 0;
?>
<?
    while ($count <= (count($_SESSION['cart']) - 1)) {
        $total += ($_SESSION['cart'][$count] -> amount * $_SESSION['cart'][$count] -> price);
        echo "<tr>";
        echo "\n<td width='100px'>".$_SESSION['cart'][$count] -> sku."</td>";
        echo "\n<td>".$_SESSION['cart'][$count] -> name."</td>";
        echo "\n<td width='100px'>".$_SESSION['cart'][$count] -> price."</td>";
        echo "\n<td width='100px'>".$_SESSION['cart'][$count] -> amount."</td>";
        echo "\n<td width='100px'>".'$'.$_SESSION['cart'][$count] -> amount * $_SESSION['cart'][$count] -> price."</td>";
        echo "\n</tr>\n";
        $count++;
    }
    echo "<tr><td></td><td></td><td></td><td></td><td><i><b>".'$'.$total."</b></i></td></tr>";
?>
 
Item.php

Code: Select all

<?
    class Item {
        var $sku;
        var $name;
        var $price;
        var $amount;
        function __construct($skuC, $nameC, $priceC, $amountC) {
            $this -> sku = $skuC;
            $this -> name = $nameC;
            $this -> price = $priceC;
            $this -> amount = $amountC;
        }
    }
?>
When I add the items using `$_SESSION['cart'][1] = new Item(1, "Test", 10.00, 5);`, it works fine, but when I do `$_SESSION['cart'][$count] = $temp;`, it does not, it creates the object but says `__PHP_Incomplete_Class_Name` instead of `Item Object`

Example:
[13] => __PHP_Incomplete_Class Object
(
[__PHP_Incomplete_Class_Name] => Item
[sku] => 444447896
[name] => Kendon
[price] => 30.00
[amount] => 6
)
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: __PHP_Incomplete_Class_Name

Post by requinix »

The problem is that the Item class isn't defined when session_start gets called. Since PHP stores object data and not the full definition, all PHP can do is create a simple class and set its data.

All you have to do is swap the two relevant lines in Cart.php.
Post Reply