PHP - Doctrine - 如何使用单个命名空间自动加载实体类

I have a group of PHP files containing classes (entities). Each class has the same namespace:

// src/App/Entity/Actions.php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Actions
 *
 * @ORM\Entity
 */
class Actions
{
// SOME CODE

I autoload the PHP files containing the classes with composer:

"autoload": {
    "psr-0": {
        "App": "src/"
    }
}

And in my bootstrap.php file, I add this line:

 use App\Entity;

So I figured that because told the app to use the App\Entity namespace, that I can just call the entity classes like this: $entity = new Actions();

but when I try that, I get this error:

Fatal error: Class 'Actions' not found in C:\wamp64\www\spider\chebi2\inc\orm_tools.php on line 49

If I do this:

use App\Entity; use App\Repository;

if (class_exists('Actions')) { dump('exists'); } else { dump('not exists'); }

if (class_exists('\App\Entity\Actions')) { dump('exists'); } else { dump('not exists'); }

Heres what it outputs:

PS C:\wamp64\www\spider\chebi2> php .\get_actions.php
"not exists"
"exists"

So it can only find the class when I provide the full namespace. And weirdly enough, when I tried this:

// Direct path to the Actions.php file
use App\Entity\Actions;

if (class_exists('Actions')) { dump('exists'); }
else { dump('not exists'); }

if (class_exists('\App\Entity\Actions')) { dump('exists'); }
else { dump('not exists'); }

I get the same result:

PS C:\wamp64\www\spider\chebi2> php .\get_actions.php
"not exists"
"exists"

So now I'm even more confused. What is the point in using: use App\Entity; if it doesn't actually make the classes in that namespace directly available? And why is assigning the direct path to the class use App\Entity\Actions; not even working?

Am I doing something wrong here? Is there a correct way to use namespaces that I'm not understanding?

PSR-0 is depracated you should use PSR-4

in PSR-4

composer.json

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

in directory src/ which is on same level as composer.json add directory Entity so in path src/Entity add class file Actions

namespace App\Entity;

class Actions
{

}

you can also use composer dump-autoload and check vendor/composer/autoload* fiels and see if namespaces are registered there.'

Regarding class_exists() it does not work with short names or aliases you need to provide the full name of class. I'd suggest using ::class operator So in your case it would be:

<?php
  use App\Entity\Actions;

  class_exists(Actions::class);

Thanks! I changed the auto loader to psr-4, and attached it to this:

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

dump-autoload is exactly what I was looking for, but I don't see any included files or classes listed:

    PS C:\wamp64\www\spider\chebi2> composer dump-autoload -vvv
    Reading ./composer.json
    Loading config file ./composer.json
    Checked CA file C:\Users\horse\AppData\Local\Temp\composer-cacert-12fdaece071ee9515fa28aabed5ab089876ae257833106e15a583e060eaff6b5.pem: valid
    Executing command (C:\wamp64\www\spider\chebi2): git branch --no-color --no-abbrev -v
    Executing command (C:\wamp64\www\spider\chebi2): git describe --exact-match --tags
    Executing command (C:\wamp64\www\spider\chebi2): git log --pretty="%H" -n1 HEAD
    Reading C:/Users/horse/AppData/Roaming/Composer/composer.json
    Loading config file C:/Users/horse/AppData/Roaming/Composer/composer.json
    Reading C:\wamp64\www\spider\chebi2/vendor/composer/installed.json
    Reading C:/Users/horse/AppData/Roaming/Composer/vendor/composer/installed.json
    Running 1.2.2 (2016-11-03 17:43:15) with PHP 5.6.25 on Windows NT / 10.0
    Generating autoload file

I still can't find the entity classes.

To clarify, I should have the folder structure like this:

 - src (contains only subdirectories)
     - Entity (contains the entity files)
     - Repositories
     - App (empty)