Friday, January 21, 2022

[FIXED] How I use composer autoloading?

Issue

I have such project https://github.com/prazny/composer-autoload

What am I doing wrong?
I would like to load class 'Writing' to composer.
index.php: Fatal error: Class 'MyApp\Writing\Writing' not found in C:\xampp\htdocs\kurs\composer-autoload\index.php on line 4


Solution

You have several issues:

  1. Problem: Class/File name mismatch. Solution: Rename ./src/Writing/Write.php to ./src/Writing/Writing.php
  2. Problem: PSR-4 autoloading standard compliance Solution: Change your PSR-4 config from "": "src/Writing/Write.php" to "MyApp\\": "src/"
  3. Then regenerate your autoload file by running composer dumpautoload

Once you do all that, you'll get a fatal error in your index.php because you're trying to echo an instance of an object that does not have a __toString. This will tell you autoloading is working and that you need to fix your index.php code.

Also, you shouldn't commit the vendor directory. That, however, is an aside and is not causing the issues you're having.



Answered By - JAAulde

No comments:

Post a Comment

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