Fatal Error: Class not Found when using namespaces - php

I have been trying to use namespaces for the first time in ages and I am running into the below problem. I am currently using Composer for a PSR-4 autoloader and I keep getting the error:
Fatal error: Class 'API\Library\Config' not found in C:\wamp64\www\project\src\index.php on line 14
composer.json
"autoload": {
"psr-4": {
"API\\": "src",
"API\\Library\\": "src/Library",
"API\\Controllers\\": "src/Application/Controllers"
}
}
src/index.php
namespace API;
include_once('vendor/autoload.php');
use API\Library\Config;
$config = new Config(); //line 18
The Folder layout is as such:

Its because src is the parent folder. Ideally vendor would be in the same directory as src.
"autoload": {
"psr-4": {
"API\\": "",
"API\\Library\\": "Library",
"API\\Controllers\\": "Application/Controllers"
}
}
Would work, or you should restructure your directories.
Also you can leave out "API\\Library\\": "Library", as it will be picked up by "API\\": "",

Related

Composer psr-4 autoload class not found error after changing prefix

i've already been looking for solutions to my problem but could'nt find any so far.
{
"name": "petersil98/thresh",
"version": "1.0.0",
"type": "library",
"autoload": {
"psr-4": {
"Thresh\\": "src/"
}
}
}
This is my public/test.php:
<?php
require_once '../vendor/autoload.php';
use Thresh\Helper\Config;
Config::setPlatform("euw1");
And this is my src/Helper/Config.php:
<?php
namespace Thresh\Helper;
class Config{...}
This is the error i get: Fatal error: Uncaught Error: Class 'Thresh\Helper\Config' not found
First i had my psr-4 autoload registered with the prefix 'src'
"autoload": {
"psr-4": {
"src\\": "src/"
}
}
After changing the psr-4 autoload prefix to Thresh (and updating the namespaces) and running composer dump-autoload it doesnt work anymore
PS: composer dump-autoload returns Generated autoload files containing 0 classes

Composer not autoloading classes in directory with same name

I am using composer to include a private package in my project that will includes some classes I will use to test against with PHPUnit. Most of the package is being autoloaded correctly and I can call the classes from my unit test, however any class that is named the same as the directory it is in is throwing a "Class not found" error.
The repository is conforming to psr-0 and is located at https://github.com/DeschutesDesignGroupLLC/IPS-Source
File structure example throwing error:
--src
----IPS
------DateTime
--------DateTime.php
Calling $date = new \IPS\DateTime; throws an error.
File structure example NOT throwing error:
--src
----IPS
------Http
--------Url.php
Calling $url = new \IPS\Http\Url; does not throw an error.
Composer.json of private package:
{
"name": "deschutesdesigngroupllc/ips",
"description": "Invision power board source files used to test against",
"homepage": "https://www.invisioncommunity.com",
"version": "4.3.6",
"autoload": {
"psr-0": {
"IPS\\": "src/"
}
},
"extra": {
"branch-alias": {
"dev-master": "4.3.6"
}
},
"require": {
"phpdocumentor/phpdocumentor": "dev-master"
}
}
In the first example, you want a file yet you give the path to it's parent. In the second, you again want a file, but this time supply the full path. Unsurprisingly, the first fails and the second succeeds.
It would appear that
$date = new \IPS\DateTime\DateTime;
is what you intended to ask for.

Composer PSR-4 autoloading "class not found" debug

Yes another question about the "class not found" error. Either I am missing something, or I misunderstood the PSR-4 logic.
My composer library directory sturcture:
"Scanner" => "src" => "Test.php"
Test.php
namespace MyNS;
class Test
{
}
composer.json
"autoload": {
"psr-4": {
"MyNS\\": "src/"
},
}
So, now I load the library in my project with composer and try using it.
require_once("../vendor/autoload.php");
$test = new MyNS\Test();
Which always results in
"Fatal error: Uncaught Error: Class 'MyNS\Test' not found."
. What am I missing? I am staring at this for days now. I have changed folders, I have changed folder names, I have changed uppper to lower and vise versa. Nothing seems to work.
I am using PHP 7.2.2 and Composer version 1.2.2
Even tried this:
require_once("../vendor/autoload.php");
use MyNS\Test;
$scanner = new Test();
Update
I debugt the Composer ClassLoader.php file (findFileWithExtension($class, $ext)) method and apparently my files are never loaded because I get put an echo "Done" and a die(); at the end of this method which means the file is not found and thus not loaded. What is wrong with my composer.json?
{
"name": "test/test",
"type": "library",
"description": "",
"keywords": ["php"],
"homepage": "",
"license": "MIT",
"authors": [
{
"name": "",
"email": "",
"homepage": "",
"role": ""
}
],
"require": {
"php": ">=7.2.2"
},
"autoload": {
"psr-4": {
"MyNS\\": "src/"
}
}
}
To debug what is happening open ClassLoader.php file then go where findFileWithExtension() method is defined to add an echo statement:
# vendor/composer/ClassLoader.php:386
foreach ($this->prefixDirsPsr4[$search] as $dir) {
if (file_exists($file = $dir . $pathEnd)) {
return $file;
}
// Add this line
echo $file, PHP_EOL;
}
Do not do composer dumpautoload after you manually modified above file until we are done.
Now by executing your PHP file you will see something similar to this at the very beginning of output:
path/to/project/vendor/composer/../../src/Test.php
Which is:
path/to/project/src/Test.php
So this is the file that composer is looking for and should contain something like this:
namespace MyNS;
class Test { }
If there is an issue in including the file then it means you have to care about three things:
Path and filename
Namespace used in file
Class name used in file (class name should be the same as filename)
i think the problem is in your namespace declaration
you calling the class from MyNS but class namespace is namespace MyNS\PSR4;
require_once("../vendor/autoload.php");
$test = new MyNS\Test();
// it should be new MyNS\PSR4\Test();
and make sure, your class file in same directory which you mentioned in composer autoload file
also you have to run dump-autoload command for any change in classnames
you can visit for this autoload feature

Class file not found by composer and psr-4

I am having very hard times understanding how to use autoloading by psr-4.After loading vagrant and setting and testing all variables in Homestead.yaml I have prepared a file structure as the following:
\app
\folder
-- test.php
\vendor
\composer
-- autoload.php
-- index.php
-- composer.json
and the following are my codes:
index.php
<?PHP
namespace app;
require('vendor/autoload.php');
$object = new folder\test();
composer.json
"autoload":{
"psr-4":{
"app\\": "app"
}
}
test.php
<?php
namespace app\folder;
class test
{
function __construct ()
{
echo 'construction done right.';
}
}
But, after trying to visit the page, these are the error message displayed on the page:
(!) Fatal error: Uncaught Error: Class 'app\folder\test' not found in /home/vagrant/web/sites/app/index.php on line 6
( ! ) Error: Class 'app\folder\test' not found in /home/vagrant/web/sites/app/index.php on line 6
Would you help me understand and fix this error?
It doesn't work because you have told Composer that the classes from the app namespace are in the app subdirectory but there is no app subdirectory.
The entire application is stored in the app directory and it's name doesn't really matter for the application. The classes of the app namespace are stored in the current directory and the sub-namespaces are stored in subdirectories with the same name.
Accordingly, your composer.json file should read:
"autoload": {
"psr-4": {
"app\\": ""
}
}
Or, to be more clear, you can put . (the current directory) as the location of the app\ namespace:
"autoload": {
"psr-4": {
"app\\": "."
}
}
After you make the change run composer dump-autoloader in the main application directory and it will start working.
To fix it for your current setup, use the following:
"autoload":{
"psr-4":{
"app\\": ""
}
}
Your composer.json is in the app-directory, so there's no subdirectory named app to reference.
I would actually recommend to change your directory structure to the following:
\app
\src
\folder
-- test.php
-- index.php
\vendor
\composer
-- autoload.php
-- index.php
-- composer.json
And then in composer.json, set the following:
"autoload":{
"psr-4":{
"app\\": "src"
}
}
This makes sure that all files belonging to your 'app' namespace are contained within a single subdirectory.
Finally, I would recommend you to use a vendor namespace to prevent conflicts, and to use the naming guidelines from PSR-2.
I think you won't need PSR-4 just add classmap
classmap parameter for example :
"autoload": {
"classmap": [
"app"
]
}
After adding this run composer dump-autoload and you should see a number of classes being added.
Hope this helps.
in composer.json try to set
"autoload":{
"psr-4":{
"": "src/"
}
}
then do execute this command
composer dump-autoload -o

php namespace class not found error

I get this error when I try to use autoload and namespaces. All my namespace classes are under app/libs/
16-Dec-2016 04:30:50 Europe/Berlin] PHP Fatal error:
Class 'App\libs\App' not found in /Users/mysite/app/page1.php on line 26
Here is My code:
require '../public/vendor/autoload.php';
use App\libs\App;
use App\libs\Auth;
class Controller
{
public $app;
public function __construct()
{
#set_exception_handler([$this, 'exceptionHandler']);
$this->app = new App();
}
}
autoload normally includes files only under vendor folders. It does not load any other files, if you do not instructe to. You are probably using composer. if it is, you can add folders in composer.json file to include class files from other folders like App\libs. An example a composer.json file is:
{
"require": {
"twig/twig": "~1.0"
},
"autoload": {
"psr-4": {
"App\\": "App/"
}
}
}
In the examle above, it will autoload any files under App folder.
Finally you need to run: composer dump-autoload to make this working.

Categories