Issue
I was making a composer library for functionality of my framework, but I got stuck on this problem, the problem is with autoloading with psr-4.
My Configuration
I have the following structure
├───src
├───test
└───vendor
├───composer
├───symfony
│ ├───polyfill-ctype
│ └───polyfill-mbstring
│ └───Resources
│ └───unidata
├───twig
│ └───twig
│ ├───doc
│ │ ├───filters
│ │ ├───functions
│ │ ├───tags
│ │ └───tests
│ └───src
│ ├───Cache
│ ├───Error
│ ├───Extension
│ ├───Loader
│ ├───Node
│ │ └───Expression
│ │ ├───Binary
│ │ ├───Filter
│ │ ├───Test
│ │ └───Unary
│ ├───NodeVisitor
│ ├───Profiler
│ │ ├───Dumper
│ │ ├───Node
│ │ └───NodeVisitor
│ ├───RuntimeLoader
│ ├───Sandbox
│ ├───Test
│ ├───TokenParser
│ └───Util
└───xenframe
└───hello
└───src
The last folder is my library with the hello folder in it.
inside of the root of the hello folder I have a composer.json and a src folder.
composer.json
{
"name": "xenframe/hello",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "XENONMC",
"email": "support@xenonmc.xyz"
}
],
"minimum-stability": "stable",
"require": {
},
"autoload": {
"psr-4": {
"hello\\": "/src"
}
}
}
Inside of the src/ folder is the main file - > index.php.
index.php
<?php
namespace xenframe\hello;
class App {
function __construct() {
echo "object constructed";
}
}
echo "hello world was loaded";
Now, the usage is in my root index.php.
index.php
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$root = str_replace('\\', '/', __DIR__);
// setup composer autoloader
require_once $root . '/vendor/autoload.php';
// visit us as https://xenonmc.xyz
use xenframe\hello\App;
$hi = new App();
What I tried
o) used psr-4 like the following "xenframe\\hello": "src/".
o) used class paths, I don't like this one because psr-4 is the recommended standard of composer.
o) using psr-0, I didn't pick this one either as there was a lot of verbose.
Thanks!
Solution
While testing the given code, the problem was resolved by renaming index.php within src to App.php which relates to class name in the file. Also used "xenframe\\hello\\": "src/" in psr-4 section in composer.json.
Also its worth mentioning that while composer was unable to autoload required class, due to PSR-4 non compliance, no error was reported by composer.
The problems were:
- The filename of class was not properly compliant with PSR-4 specifications. From section 2.3.3
The terminating class name corresponds to a file name ending in
.php. The file name MUST match the case of the terminating class name.
- The
namespacementioned inpsr-4section incomposer.jsonwas not matching thenamespaceused in class file. From Composer's PSR-4 Schema
Under the
psr-4key you define a mapping from namespaces to paths, relative to the package root. When autoloading a class likeFoo\\Bar\\Baza namespace prefixFoo\\pointing to a directorysrc/means that the autoloader will look for a file namedsrc/Bar/Baz.phpand include it if present.
Answered By - Umair Khan
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.