0

I'm very new in the field and I'm trying to create my first composer package. I'm following the structure mentioned here but for some reason I always get that the class is not found.

My directory structure is

Project
 - src/
   -- project
      index.php
 - vendor/
   -- composer/
   autoload.php
index.php

So in the main directory Project I have index.php with

<?php
use App\project;

// Autoload files using the Composer autoloader.
require_once __DIR__ . '/vendor/autoload.php';

$entry = new simplePrint();
echo($entry->printHome());

In the directory src/project/ I have index.php with

<?php

namespace App\project;

class simplePrint {

    public function printHome() {
        return "Hey";
    }
}

in composer.json

    "autoload": {
        "psr-4": {
                "App\\": "src/"
        }
    }

After I create the files, I've made

composer install 
composer dump-autoload

What I'm missing here?

Update: after composer update it is still same. The output of the composer update

$ composer update
Loading composer repositories with package information
Updating dependencies
Nothing to modify in lock file
Installing dependencies from lock file (including require-dev)
Nothing to install, update or remove
Generating autoload files

127.0.0.1:45046 [500]: GET / - Uncaught Error: Class "App\project" not found in ...

Rob
  • 15
  • 5

1 Answers1

0

PSR-4 says following: The terminating class name corresponds to a file name ending in .php. The file name MUST match the case of the terminating class name.

So you must call your filename simplePrint.php and not index.php.

Here you can read some more information about PSR-4: https://www.php-fig.org/psr/psr-4/

stefket
  • 774
  • 5
  • 12
  • Okay, I missed this. I've changed the filename inside the `src/project` from `index.php` to `simplePrint.php`. Still same error. – Rob Nov 10 '22 at 14:00
  • 1
    I have seen, that your use statement is wrong. Instead of `use App\project;` it must be `use App\project\simplePrint;` – stefket Nov 10 '22 at 14:05
  • 1
    That's it! ... along with the filename. Thanks! – Rob Nov 10 '22 at 14:09