PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, February 22, 2022

[FIXED] Problem autoloading class PHP Slim framwork

 February 22, 2022     autoload, composer-php, php, slim     No comments   

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
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

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

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing