Skip to content
Advertisement

Composer psr-4 autoload issue

I have problem with autoloading with composer when i use psr-4 autoloading it doesn’t work and give me error.

I tried:

JavaScript

and a lot of other thing but it doesn’t work without

JavaScript

error:

JavaScript

composer.json:

JavaScript

greeting.php (file with class to load):

JavaScript

index.php file:

JavaScript

Advertisement

Answer

It is generally good practice to capitalize the first letter of a class name. It also adheres with the rules of PSR-1.

Change your composer.json file to look like this:

JavaScript

Now, in your index file. We are going to import the autoloader. To do this simply require it:

require 'vendor/autoload.php';

Now that you have included the autoloader, go into every class and set the namespace.

The classes in your src/ == namespace One;

Check your classes in src/ and make sure they are all namespaced. Meaning that they should all have the following line of code at the top:

namespace One;

As mentioned before, update your file names to Foo.php and class names to class Foo to adhere to PSR. (This is not required but highly recommended and standard procedure.)

To use one of your classes you would say use OneGreeting;

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement