I'm new to using composer for autoloading custom class files using 'PSR-0' way of autoloading classes.
Below is my directory tree structure.
And below is my PSR-0 config in the composer.json file
Class autoloading is fine when requested from outside my project file after requiring the vendor/autoload.php
The problem is when trying to load a class from within another class with a defined namespace.
Example:
I am trying to call a static method ::GetDatabaseConfig() from the Config class within the Config folder into the Database class within the Database folder. This is the code I'm using inside the Database class.
Database/Database.php
namespace App\Database;
class Database{
public static $con;
public static $connected = false;
public static $error = false;
public static $error_message = "";
function __construct( $opt=false ){
$config = App\Config\Config::GetDatabaseConfig();
//REST....
}
}
This code doesn't work and displays this error.
Fatal error: Uncaught Error: Class "App\Database\App\Config\Config" not found in /storage/emulated/0/code/durandal/htdocs/api/idan/App/Database/Database.php:13
I see that the namespace used in the Database.php file is used as a prefix when requesting App\Config\Config class.
What is it that I'm missing here? How to resolve it?
You're not using a FQCN of the Config
class, but a name relative to App\Database
namespace.
To fix this, you need to prefix the Config
class with a \
:
\App\Config\Config::GetDatabaseConfig();
or even better, you can import the Config
class with use App\Config\Config;
and then use it with Config::GetDatabaseConfig()