0

I always get the class not found exception in php laravel 6 when i create a class and extend a parent class named A which is located in the same directory.

However, another child class that is located in same directory could extend class A successfully.

In addition, i couldn't also instantiate the class A due to class not found exception in another .php file.

Please help me on this. Thanks in an advance.

Parent class: myContext

<?php
namespace config\models;

class myContext {
    public static $conn;
    ...
}

Class myUser: extension is fine.

<?php
namespace config\models;

class myUser extends myContext {
   private $name;
   ...
}

Class oauth: extension returns myContext class not found.

<?php
namespace config\models;

class oauth extends myContext {
    private $user;
}

Instantiate the class - returns class not found.

<?php

use config\models\myContext as context;

$cont = new context();

enter image description here

Wilson
  • 39
  • 6

5 Answers5

0

Check whether the namespace is added correctly when the parent class is imported.

Refer

https://www.quora.com/What-is-namespaces-and-use-in-Laravel

external classes and PHP files in Laravel - Class not found

Laravel - Model Class not found

Laravel 6: Class 'Form' not found

Techie
  • 44,706
  • 42
  • 157
  • 243
0

In composer.json file,

"autoload": {
    "psr-4": {
        "App\\": "app/",
        "config\\models\\": "config/models"
    },
    "classmap": [
        "database/seeds",
        "database/factories"
    ]
},

After that composer dump-autoload.

Devansh
  • 52
  • 7
  • First of all, you have not shared any codes. Then after sharing code you down voted. Class Not Found Error always come from these two things. Either you forgot to dump autoloads. Please check your code. – Devansh Jun 04 '20 at 03:32
  • And by the way, you are using your models outside the app folder. Due to which Laravel not recognizing it. For that, you need to update the composer file 'PSR-4' key. To tell Laravel that I want to use config folder. – Devansh Jun 04 '20 at 03:38
  • I tried to follow what you instructed, but still no use. In the end, i decided to move the model to App directory and dump-autoload after that. It now works fine. Thanks for your help. – Wilson Jun 04 '20 at 04:38
  • Now, this is the right thing, as per the convention you need to use all your codes inside the app folder. If you want to use another folder than you need to update the composer.json file and then in each file you need to set namespace according to your registry. – Devansh Jun 04 '20 at 08:08
0

To get the provided example codes to work you need to use require_once

<?php
require_once('models/myContext.php');

use app\config\models\myContext as context;

$test = new context();

A search on SO brought me to this source

Florian
  • 845
  • 1
  • 9
  • 17
  • Thanks for it. but it doesn't work. Below error was returned. Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_ERROR) Class 'app\config\models\myContext' not found – Wilson Jun 03 '20 at 08:07
  • You might need to update the `use app\conig....` path according to your setup – Florian Jun 03 '20 at 08:09
  • if i `use config\models\myContext`, i can browse myContext class, or else i couldn't. – Wilson Jun 03 '20 at 08:18
  • Please update the paths in the example to fit your setup. Try to get the path to work for you. We are trying to help you but can not provide a copy and paste working answer without access to your entire codebase. – Florian Jun 03 '20 at 08:35
  • using these 2 lines `require_once('models/myContext.php');` `use config\models\myContext as context;` returns below error `Fatal error: Cannot declare class config\models\myContext, because the name is already in use` – Wilson Jun 04 '20 at 04:03
  • You need to put some work and knowledge in this one to get it work with your code. I don't know which path is the correct path for your codebase, for me these lines worked. Your setup might differ so you need to adjust the paths, maybe multiple times. This error is always related to your specific directory layout and codes, which we don't have access to, and don't want access to. – Florian Jun 04 '20 at 05:13
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/215274/discussion-between-florian-and-wilson). – Florian Jun 04 '20 at 05:16
  • That's ok. i managed to get it work now. Thanks for your efforts. – Wilson Jun 04 '20 at 05:44
0

You try to add this line of code in the composer.json file, then execute the composer dumpautoload command on the command line

enter image description here

Dcynsd
  • 41
  • 1
-1

have you add autoload for config/ folder in composer.json. Laravel only default autoload for app/

Thai Nguyen Hung
  • 1,154
  • 5
  • 13
  • added the autoload for config/ folder. but it returned an error telling i can't declare class as it's already in use after i ran dump-autoload. – Wilson Jun 03 '20 at 08:15