Tuesday, February 22, 2022

[FIXED] Problem autoloading class PHP Slim framwork

Issue

I'm facing a problem with my slim(http://www.slimframework.com/) application. When I'm trying to load my Database class using namespacing I get this error:

Message: Class 'Craft\Code\CraftDB\Database' not found

I have Database.php in folder app/config/Database.php

My Database class start like this

namespace Craft\Code\CraftDB;

class Database {

I'm trying to use it like this in another file:

use Craft\Code\CraftDB as DB;
class MyOtherClass {
    protected $connectDb;
    protected $db;
    public function __construct() {
        $this->connectDb = new DB\Database;
        $this->db = $connectDb->connect();
    }

My Composer file is :

"autoload": {
        "psr-4": {
            "Craft\\Code\\": "app/"
        }
    }

I'm trying to find the problem but I'm lost now. Please help. Thanks


Solution

Where you say your file is: app/config/Database.php

Where you tell Composer your file is: app/CraftDB/Database.php

Things simply don't seem to match here. On file system side you have that config level unaccounted for, on namespace side CraftDB level in namespace.

From your description I imagine you need something like:

"autoload": {
        "psr-4": {
            "Craft\\Code\\CraftDB\\": "app/config/"
        }
    }


Answered By - Rarst

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.