Home > front end >  Class not found in the namespace
Class not found in the namespace

Time:11-11

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 ...

CodePudding user response:

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/

  •  Tags:  
  • php
  • Related