Skip to content
Advertisement

Why is PHP saying my class name is already being used when It’s the first time I’ve declared it

Apologies for the long code, it’s 4 files but I have no choice since the problem can be any where. Thankfully I’ve found the solution I’m just trying to figure out why this had happened in the first place, so that will probably cut down the time significantly.

classes/Cart.php

<?php

require './classes/CartItem.php';




class Cart
{

    var int $id;
    var array $listoforderitem = [];



    function addProduct($productt, $quantityy)
    {

        if ($productt->stock < $quantityy) {

            print("We don't have that many stock !");
        } else {

            $productt->stock -= $quantityy;

            $newcartitem = new CartItem($productt, $quantityy);


            array_push($this->listoforderitem, $newcartitem);

            // var_dump(count(($this->listoforderitem)));


            return $newcartitem;
        }
    }

    function getTotalQuantity()
    {

        $quantityy = 0;

        for ($i = 0; $i < count($this->listoforderitem); $i++) {

            $quantityy += $this->listoforderitem[$i]->quantity;

            // var_dump($quantityy);
        }

        return $quantityy;
    }

    function getTotalSum()
    {

        $sum = 0;

        for ($i = 0; $i < count($this->listoforderitem); $i++) {

            $orderitemsum = $this->listoforderitem[$i]->quantity * $this->listoforderitem[$i]->product->price;

            $sum += $orderitemsum;

            // var_dump($sum);
        }

        // var_dump(0 < count($this->listoforderitem));

        return $sum;
    }
}

classes/CartItem.php

<?php


class CartItem
{
    var object $product;
    var int $quantity;


    function __construct(object $product, int $quantity)
    {


        $this->product = $product;
        $this->quantity = $quantity;
    }


    function increaseQuantity(): void
    {

        if ($this->product->stock < 1) {
            print("We don't have that many stock !");
        } else {

            $this->product->stock -= 1;

            $this->quantity += 1;
        }
    }
}

classes/Product.php

    <?php


require_once './classes/CartItem.php';


class Product
{

    var int $id;
    var string $name;
    var int $price;
    var int $stock;


    function __construct(int $id, string $name, int $price, int $stock)
    {
        $this->id = $id;
        $this->name = $name;
        $this->price = $price;
        $this->stock = $stock;
    }

    function addToCart($cart, $quantityy)
    {

        if ($quantityy > $this->stock) {

            print("We don't have that many stock !");
        } else {

            $this->stock -= $quantityy;

            $newcartitem = new CartItem($this, $quantityy);



            array_push($cart->listoforderitem, $newcartitem);

            // var_dump(count($cart->listoforderitem));

            return $newcartitem;
        }
    }
}

./index.php

require_once './classes/Product.php';
require_once './classes/Cart.php';


$product1 = new Product(1, "iPhone 11", 2500, 10);
$product2 = new Product(2, "M2 SSD", 400, 10);
$product3 = new Product(3, "Samsung Galaxy S20", 3200, 10);
$cart = new Cart();
$cartItem1 = $cart->addProduct($product1, 1);
$cartItem2 = $product2->addToCart($cart, 1);
echo "Number of items in cart: " . PHP_EOL;
echo $cart->getTotalQuantity() . PHP_EOL; // This must print 2
echo "Total price of items in cart: " . PHP_EOL;
echo $cart->getTotalSum() . PHP_EOL; // This must print 2900

$cartItem2->increaseQuantity();
$cartItem2->increaseQuantity();

echo "Number of items in cart: " . PHP_EOL;
echo $cart->getTotalQuantity() . PHP_EOL; // This must print 4

echo "Total price of items in cart: " . PHP_EOL;
echo $cart->getTotalSum() . PHP_EOL; // This must print 3700


echo "Number of items in cart: " . PHP_EOL;
echo $cart->getTotalQuantity() . PHP_EOL; // This must print 4

echo "Total price of items in cart: " . PHP_EOL;
echo $cart->getTotalSum() . PHP_EOL; // This must print 3700

So if I were to run the code on index.html it gave me this error

Fatal error: Cannot declare class CartItem, because the name is already in use in C:codingappsxampphtdocsoop_cart_logicclassesCartItem.php on line 4

which is weird because I’ve never declared it anywhere else.

now the solution for this it to shuffle the required_once from this:

require_once './classes/Product.php';
require_once './classes/Cart.php';

to this:

require_once './classes/Cart.php';
require_once './classes/Product.php';

and it worked perfectly! any idea of why this had happened?

Advertisement

Answer

I think this was caused by me using require instead of require_once in Cart so if class Product comes first then the class CartItem will be redeclared since class Cart did not check if CartItem was already declared in the code.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement