Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

heropaulexy's avatar

PHP error

i am having this error

Fatal error: Uncaught Error: Class 'app\controllers\Product' not found in C:\xampp\htdocs\projects\product-crud\better\controllers\MainController.php:32 Stack trace: #0 [internal function]: app\controllers\MainController::create(Object(app\router)) #1 C:\xampp\htdocs\projects\product-crud\better\router.php(32): call_user_func(Array, Object(app\router)) #2 C:\xampp\htdocs\projects\product-crud\better\public\index.php(18): app\router->resolve() #3 {main} thrown in C:\xampp\htdocs\projects\product-crud\better\controllers\MainController.php on line 32

0 likes
21 replies
Sinnbeck's avatar

Yes? Do you have some code we can see or do you need an explanation of the error?

heropaulexy's avatar

@Sinnbeck

<?php

namespace app\controllers;

use app\Router;

class MainController
{
    public function index(Router $router){ //$router -> renderViews
        $search = $_GET['search'] ?? '';
        $products = $router -> db ->getProducts($search);
        $router -> renderViews('products/index', [
            'products' => $products,
            'search' => $search
        ]);
    }
    public function create(Router $router)
    {
        $errors = [];
        $productData = [
            'title' => '',
            'description' => '',
            'image' => '',
            'price' => '',
        ];
        if ($_SERVER['REQUEST_METHOD'] === 'POST'){
            $productData['title'] = $_POST['title'];
            $productData['description'] = $_POST['desc'];
            $productData['imageFile'] = $_FILES['image'] ?? null;
            $productData['price'] = (float)$_POST['price'];

            $product = new Product();
            $product->load($productData);
            $errors = $product -> save();
            if (empty($errors)){
                header('Location: /products');
                exit();
            }

        }
        $router -> renderViews('products/create', [
            'product' => $productData,
            'errors' => $errors
        ]);
    }
    public function update(){
        echo "Update Page";
    }
    public function delete(){
        echo "Delete Page";
    }
}
Sinnbeck's avatar

@heropaulexy I assume you are missing an import for this

$product = new Product();

Maybe

use app\models\Product;
heropaulexy's avatar

@Sinnbeck it is still returning this on the browser after i imported use app\models\products

Fatal error: Uncaught Error: Class 'app\controllers\Product' not found in C:\xampp\htdocs\projects\product-crud\better\controllers\MainController.php:33 Stack trace: #0 [internal function]: app\controllers\MainController::create(Object(app\router)) #1 C:\xampp\htdocs\projects\product-crud\better\router.php(32): call_user_func(Array, Object(app\router)) #2 C:\xampp\htdocs\projects\product-crud\better\public\index.php(18): app\router->resolve() #3 {main} thrown in C:\xampp\htdocs\projects\product-crud\better\controllers\MainController.php on line 33

Sinnbeck's avatar

@heropaulexy you imported products not Product. Can you see the difference?

Also the app\models part is just a guess. I don't know your files

Sinnbeck's avatar

@heropaulexy then you actually need to call that in your code. Php has no way of knowing that product and products are the same thing

$product = new products();
Sinnbeck's avatar

Just a suggestion. Stick to one naming convention for your classes. If some classes are StudlyCase and others are lowercase, it will get quite confusing

heropaulexy's avatar

@Sinnbeck here is the models post

<?php

namespace app\models;

class products
{
    public ?int $id = null;
    public ?string $title = null;
    public ?string $description = null;
    public ?string $imagePath = null;
    public ?float $price = null;
    public ?array $imageFile = null;

    public function load($data){
        $this -> id =$data['id'] ?? null;
        $this -> title = $data['title'];
        $this -> description = $data['description'] ?? '';
        $this -> price = $data['price'];
        $this -> imagePath = $data['image'] ?? null;
        $this -> imageFile = $data['imageFile'] ?? null ;
    }

    public function save(){
        $errors = [];
        if (!$this -> title){
            $errors[] = 'Product title is required';
        }
        if (!$this -> price){
            $errors[] = 'Product price is required';
        }
        if (!is_dir(__DIR__.'/../public/images')){
            mkdir(__DIR__.'/../public/images');
        }
        if (empty($error)) {

            if ($this -> imageFile && $this -> imageFile['tmp_name']) {

                if ($this -> imagePath) {
                    unlink(__DIR__.'/../public/'.$this -> imagePath);
                }

                $this -> imagePath = 'images/' . randomString(8) . '/' . $this -> imageFile['name'];
                mkdir(dirname(__DIR__.'/../public/'.$this -> imagePath));
                move_uploaded_file($this -> imageFile['tmp_name'],__DIR__.'/../public/'. $this -> imagePath);
            }

            $db = $Database::$db;
            if ($this -> id){
                $db ->updateProduct($this);
            }
            else{
                $db -> createProduct($this);
            }
        }
        return $errors;
    }
}

Sinnbeck's avatar

@heropaulexy Ok. If you are still getting an error, tell us what the error is and what you code is now

heropaulexy's avatar

@Sinnbeck That error is gone after i changed the name to products but i have another error.

Fatal error: Uncaught Error: Class 'app\models\Database' not found in C:\xampp\htdocs\projects\product-crud\better\models\products.php:47 Stack trace: #0 C:\xampp\htdocs\projects\product-crud\better\controllers\MainController.php(35): app\models\products->save() #1 [internal function]: app\controllers\MainController::create(Object(app\router)) #2 C:\xampp\htdocs\projects\product-crud\better\router.php(32): call_user_func(Array, Object(app\router)) #3 C:\xampp\htdocs\projects\product-crud\better\public\index.php(18): app\router->resolve() #4 {main} thrown in C:\xampp\htdocs\projects\product-crud\better\models\products.php on line 47

<?php

namespace app\models;

class products
{
    public ?int $id = null;
    public ?string $title = null;
    public ?string $description = null;
    public ?string $imagePath = null;
    public ?float $price = null;
    public ?array $imageFile = null;

    public function load($data){
        $this -> id =$data['id'] ?? null;
        $this -> title = $data['title'];
        $this -> description = $data['description'] ?? '';
        $this -> price = $data['price'];
        $this -> imagePath = $data['image'] ?? null;
        $this -> imageFile = $data['imageFile'] ?? null ;
    }

    public function save(){
        $errors = [];
        if (!$this -> title){
            $errors[] = 'Product title is required';
        }
        if (!$this -> price){
            $errors[] = 'Product price is required';
        }
        if (!is_dir(__DIR__.'/../public/images')){
            mkdir(__DIR__.'/../public/images');
        }
        if (empty($error)) {

            if ($this -> imageFile && $this -> imageFile['tmp_name']) {

                if ($this -> imagePath) {
                    unlink(__DIR__.'/../public/'.$this -> imagePath);
                }

                $this -> imagePath = 'images/' . randomString(8) . '/' . $this -> imageFile['name'];
                mkdir(dirname(__DIR__.'/../public/'.$this -> imagePath));
                move_uploaded_file($this -> imageFile['tmp_name'],__DIR__.'/../public/'. $this -> imagePath);
            }

            $db = Database::$db;    
            if ($this -> id){
                $db ->updateProduct($this);
            }
            else{
                $db -> createProduct($this);
            }
        }
        return $errors;
    }
}
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@heropaulexy Happy to help. If it has been solved, you can close the thread by marking a best answer

Please or to participate in this conversation.