I have just started a new PHP project and right up front, I am running into issues with the autoloader. I searched for the error and consulted the documentation (http://www.php-fig.org/psr/psr-4/), but the issue persists.
Hence, I created a minimal example to narrow down the cause of the error - yet, even with this minimal example, it wont work :(
My folder structure is like this:
+ src/
| + Xyz.php
+ composer.json
+ test.php
Here is my code
composer.json:
{
"name": "sg/ABC",
"description": "abc",
"autoload": {
"psr-4": {
"sg\\ABC\\": "src/"
}
}
}
Xyz.php:
<?php namespace sg\ABC;
class Xyz
{}
?>
test.php:
<?php namespace sg\ABC;
use sg\ABC\Xyz;
$a = new Xyz();
?>
Even though running composer install shows no errors, I immediately get this error when running the code:
$ php test.php
PHP Fatal error: Class 'sg\ABC\Xyz' not found in /dir/x/test.php on line 5
Fatal error: Class 'sg\ABC\Xyz' not found in /dir/x/test.php on line 5
also, running composer dump-autoload (as suggested here and there in this board) does not help
You still need to include the composer autoload.php file to include the loaded libraries.
You need to require the autoloader .. it is usually require_once "path/to/vendor/autoload.php".
Related
i have maybe primitive problem. I created my first package in composer. It is just one class in one namespace.
composer.json:
...
"autoload": {
"psr-4": {
"UrlParser\\": "src/"
}
},
...
and i have this in: src/UrlParser/url.php
<?php
namespace UrlParser;
class Url{
...
everything is OK, i uploaded my package into composer. I install it into my project, but when i call this:
<?php
require_once 'vendor/autoload.php';
$a = new UrlParser\Url("http://localhost/aaa.html");
i get this: Fatal error: Class 'UrlParser\Url' not found in C:\xampp\htdocs\ccc\01\index.php on line 3
I am new in composer and i try to google my problem, but i am lost :)
Thanks
Try this
namespace UrlParser;
$a = new Url("http://localhost/aaa.html");
If it doesn't work, there's probably a problem with autoload that you didn't get the right way
the problem was, that i didnĀ“t do this:
composer dump-autoload -o
I hate to add more to the noise of "autoload isn't working!!!!!", but I can't seem to get this problem figured out, and I figured getting some fresh eyes on it would get the problem in a lot less time. Here is my index.php file:
<?php
declare(strict_types=1);
require_once 'vendor/autoload.php';
require_once 'model/PageNav.php';
use ShinePHP\{Crud, CrudException, EasyHttp, EasyHttpException, HandleData, HandleDataException};
// ALWAYS serve over encrypted channels
try {
EasyHttp::checkHttps();
} catch (EasyHttpException $ex) {
echo $ex;
}
try {
// check if it's a GET request, if it is, serve page, if not, do nothing
if (EasyHttp::isRequestMethod('GET')) {
$Page = new PageNav('Home', 'view/home.php');
$Page->buildPage();
exit;
}
}
catch (EasyHttpException $ex) {
echo $ex;
}
So obviously I am using a package from composer called ShinePHP (it's one I've made, and I'm still working on the documentation, so I'm just using it for my own projects at the moment, composer just makes package management so easy!)
ANYWAYS...because I'm writing this question, I'm obviously getting the following error:
Fatal error: Uncaught Error: Class 'ShinePHP\EasyHttp' not found in /Applications/XAMPP/xamppfiles/htdocs/KRTY/src/index.php:11 Stack trace: #0 {main} thrown in /Applications/XAMPP/xamppfiles/htdocs/KRTY/src/index.php on line 11
Now, I haven't manually touched the composer.json file, so here it is:
{
"require": {
"adammcgurk/shine-php": "~0.0.1"
},
"autoload": {
"psr-4": {
"ShinePHP\\": "src/"
}
}
}
I'm not getting any errors on requiring the vendor/autoload.php file (and I've tried changing the path to something that doesn't exist like vendor/alkdjfladksf/autoload.php and it throws an error how it should), I'm running PHP version 7.2.7 on XAMPP on Mac OS Mojave. Here is the directory structure, the highlighted index.php file is the one with the code above:
And here is the output of composer dump-autoload -o:
Generating optimized autoload files
And so...to add more to the fire of this question on Stack...How can I get composer to autoload my ShinePHP namespace with the classes as shown in the code?
This dependency does not have any autoloading rules, so Composer does not know where to find ShinePHP\EasyHttp class. You need to add autoloading configuration in composer.json of shine-php package:
"autoload": {
"psr-4": {
"ShinePHP\\": "src/"
}
},
I try to autoload my custom pdo class with composer.
Ran the following command to update autoload:
compser update
composer install
Both seem to work, no error prompted. But,
vendor/composer/autoload_namespaces.php
Does not list the custom namespace added to composer.js.
File structure
-Root
->classes
->pdo
->class.php
->vendor
->various extensions loaded with composer
index.php
PHP Class
namespace Classes\Pdo;
Class DB {
//Do some stuff...
}
Composer.js
"autoload": {
"psr-4": {
"Classes\\Pdo\\": "classes/pdo"
}
}
Index.php
$pdo = new \Classes\Pdo\DB(); //Fatal error: Class 'Classes\Pdo\DB' not found
Old question, but I just ran across this myself.
For future Googlers, in my case the issue turned out to be the name of the class file did not exactly match the class name.
See this post: Why does 'composer dumpautoload -o' fix 'Class not found' PHP error?
I am relatively new to using composer and autoload to organize my code. I have a git repository and on my local machine, I set up composer in the root dir of my project. I specified everything in a composer.json needed to get running. Using "composer install", all libraries are automatically installed.
{
"name": "my/repo",
"description": "bla",
"version": "1.2.3",
"require":
{
"php": "5.6.*",
"geraintluff/jsv4": "1.*",
"lcobucci/jwt": "^3.0"
},
"autoload":
{
"psr-4":
{
"MyNamespace\\": "src/"
}
}
}
So - once I ran "composer install" on my local machine, everything was autoloaded in my code. Fine.
But now I need to deploy the whole thing on another linux system. So i pull from the git and run composer install. All the libraries are fetched and the autoload file shows up in vendor/
Yet, I cannot use autoload (yes, I did require_once(__DIR__ . '/../vendor/autoload.php');). Everytime I try to instantiaze a class, i get a
PHP Fatal error: Class 'X' not found in /var/www/bla/x.class.php on line 123
Using use X; does not solve the problem, nor does trying to instantiate the class with its full Namespace name (e.g. $x = new \A\B\X();)
Here is the folder structure (if this matters):
+ src/
| + X.class.php // namespace here is "MyNamespace"
| + Y.class.php // same namespace
+ test/
+ run.php // namespace is "Test"
Here is a snippet of this code (run.php):
<?php namespace Test; // different namespace than the rest of the code
// making the namespace also "\MyNamespace" wouldnt work either
require_once(__DIR__ . '/../vendor/autoload.php');
use \MyNamespace\Y; // whether this line is here or not does not change the error
session_start();
// same error as with "just" implements Y {}
class SomeClass implements \MyNamespace\Y {
// ...
}
?>
So here, the Fatal error is thrown for the line where Y is extended. No matter if I use the full namespace or not. Only thing that will help is a require_once()...
So, this forces me to go back to the cumbersome way of doing all the require/includes myself!? Is there any way to solve this?
PS: composer dumpautoload wont help
PPS: composer validate shows no errors
For PSR-4 compliance, your file structure should be:
+ src/
| + X.php
| + Y.php
Note the removal of the .class.php suffix. http://www.php-fig.org/psr/psr-4/
Hello guys i have problem with autoloading my class with composer. On Linux all work perfect, but now my boss change env and set Windows. All this work on linux but windows show newbie fatal error:
Fatal error: Class 'AbstractController' not found in
D:\xampp\htdocs\ikacFw\frontController.php on line 7
Common to see my composer.json and stucture for better picture on problem.
Stucture is :
frontController.php
-- vendor
----- Doctrine
----- Ikac
--------- Components
---------- Mvc
------------- Controller
Am trying to load all data from vendor directory.
Composer.json
{
"autoload": {
"psr-0": {
"vendor": ""
}
}
}
Also new component i add manual. Like this :
$loader = require_once 'vendor/autoload.php';
$loader->add('vendor', "Ikac");
Okay next when i try to call :
<?php
require_once 'vendor/autoload.php';
use Ikac\Mvc\Controller;
$a = new AbstractController();
I get error "not found".
My class AbstractController contain defined namespace but dont work again. Like test i do this:
<?php
//vendor/Ikac/Mvc/Controller/AbstractController.php
namespace Ikac\Mvc\Controller;
class AbstractController {
function __construct() {
echo __CLASS__;
}
}
?>
I do from cmd composer dump-autoload, install, but dont work. All this perfect work on linux but here wont. Any idea how to fix this or where i do mistake.
Thanks guys!
SLOVED:
{
"autoload": {
"psr-0": {
"": "vendor/"
}
}
}
Well you should do
<?php
require_once 'vendor/autoload.php';
use Ikac\Mvc\Controller\AbstractController;
$a = new AbstractController();
Your autoloading declaration is wrong.
You will NEVER ever need to include the vendor folder in any autoloading. The vendor folder will contain the autoloading for both all dependencies, and - if configured - for your own classes as well.
You can use Composer to create autoloading for your own classes. Just include the correct info. But from your current info I cannot deduct what would be correct.