2016-11-26 74 views
0

我有一组包含类(实体)的PHP文件。每个类都有相同的命名空间:PHP - Doctrine - 如何自动加载实体类使用单个命名空间

// src/App/Entity/Actions.php 

namespace App\Entity; 

use Doctrine\ORM\Mapping as ORM; 

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

我自动加载包含与作曲家类的PHP文件:

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

而且在我的bootstrap.php文件,我加入这一行:

use App\Entity; 

所以我想,因为告诉应用程序使用App \ Entity命名空间,我可以像这样调用实体类:$entity = new Actions();

但是当我尝试,我得到这个错误:

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

如果我这样做:

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'); } 

继承人它所输出:

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

所以它只能找到当我提供完整的命名空间的时候。而古怪的是,当我尝试这样做:

// 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'); } 

我得到了相同的结果:

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

所以现在我更糊涂了。如果它实际上没有使该名称空间中的类直接可用,那么使用什么要点:use App\Entity;?为什么分配直接路径use App\Entity\Actions;甚至不能正常工作?

我在这里做错了什么?有没有一种正确的方法来使用我不理解的命名空间?

回答

1

PSR-0 depracated你应该使用PSR-4

PSR-4

composer.json

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

在src /目录是在同一水平作曲家.json添加目录Entity所以在路径src/Entity添加类文件Actions

namespace App\Entity; 

class Actions 
{ 

} 

您还可以使用composer dump-autoload并检查供应商/作曲家/自动加载* fiels并查看名称空间是否在此处注册。

关于class_exists()它不适用于短名或别名,您需要提供类的全名。我建议使用::class运营商因此,在您的情况下它将是:

<?php 
    use App\Entity\Actions; 

    class_exists(Actions::class); 
0

谢谢!我改变了自动加载器PSR-4,它连接到这一点:

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

转储自动加载正是我一直在寻找,但我没有看到任何包含列出的文件或类:

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 

我还是找不到实体类。

为了澄清,我应该有文件夹结构是这样的:

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

看看'“/vendor/autoload.php”;' – Robert

+0

的autoload.php文件本身只有两个在它的线条,但我检查了vendor/composer/autoload_psr4.php文件,它列出了** composer.json **文件中的所有软件包,但没有列出应该使用psr4自动加载器自动加载的任何文件。在** autoload_namespaces.php **中,它不会列出我添加的任何名称空间。当我运行** composer diagnose **时,其中一行是这样的:'属性psr-4没有被定义,并且定义不允许附加属性' –

+0

所以在读完一些东西之后,似乎我误解了'使用某些\命名空间'命令是做什么的。我认为'使用App \ Entity;'会将该名字空间中每个类的名称加载到全局变量中,但实际上做的是创建为名为Entity的别名。所以相反,我需要像这样调用类\ Entity \ Actions。我不明白它,因为我通过作曲家安装了很多不同的组件,我添加了'use path \ to \ namespace;',然后我可以直接调用这些类。 –

相关问题