PSR4 Composer Autoload Fatal error: Trait not found

PSR4 Composer Autoload Fatal error: Trait not found

我正在尝试将此错误处理 class 实施到我的 php 网站 https://github.com/niteshapte/advanced-error-exception-handler-logger-php 但我一直收到错误

PHP Fatal error: Trait 'SingletonTrait' not found in www.example.com/application/core/ErrorExceptionHandler.php on line 37.

第 37 行是第二个 use SingletonTrait; 语句。

我刚刚开始思考名称空间,然后 traits 出现了!

我的代码在下面....为了简洁起见,我删除了很多代码,但完整的代码可以在上面的 github URL 查看。

你能看出我做错了什么吗?

来源 www.example.com/application/core/ErrorExceptionHandler.php

<?php
namespace Utilities;
use SingletonTrait;

if(!defined('DIRECT_ACCESS')) {
    die("Direct access is forbidden.");
}

class ErrorExceptionHandler implements IUtilities {

    // Singleton instance - the line below generates the error
    use SingletonTrait;
....
}

来源 www.example.com/application/core/SingletonTrait.php

<?php
namespace Utilities;
use FrameworkException;

if(!defined('DIRECT_ACCESS')) {
    die("Direct access is forbidden.");
}

trait SingletonTrait {
.....
}

来源 www.example.com/application/core/FrameworkException.php

<?php
namespace Utilities;

if(!defined('DIRECT_ACCESS')) {
    die("Direct access is forbidden.");
}

class FrameworkException extends \Exception { }

来源 www.example.com/application/core/IUtilities.php

<?php
namespace Utilities;

if(!defined('DIRECT_ACCESS')) {
    die("Direct access is forbidden.");
}

interface IUtilities { }

来源 www.example.com/index.php

<?php
require '../vendor/autoload.php';

use Utilities\ErrorExceptionHandler;
define("DIRECT_ACCESS", TRUE);
ErrorExceptionHandler::getInstance();

这是我的 www.example.com/composer.json

"autoload": {
    "psr-4": { "": ["application/core/", "application/model/"],
               "Utilities\": "application/core/",
               "SingletonTrait\": "application/core/",
               "FrameworkException\": "application/core/"
    }
}

将其上传到我的服务器后,我在我的 PuTTY 命令行中发出 composer dump-autoload

这会在 www.example.com/vendor/composer/autoload_psr4.php

生成以下源
<?php

// autoload_psr4.php @generated by Composer

$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);

return array(
    'phpDocumentor\Reflection\' => array($vendorDir . '/phpdocumentor/reflection-common/src', $vendorDir . '/phpdocumentor/type-resolver/src', $vendorDir . '/phpdocumentor/reflection-docblock/src'),
    'Webmozart\Assert\' => array($vendorDir . '/webmozart/assert/src'),
    'Utilities\' => array($baseDir . '/application/core'),
    'Symfony\Component\Yaml\' => array($vendorDir . '/symfony/yaml'),
    'SingletonTrait\' => array($baseDir . '/application/core'),
    'Gregwar\Captcha\' => array($vendorDir . '/gregwar/captcha'),
    'FrameworkException\' => array($baseDir . '/application/core'),
    'Doctrine\Instantiator\' => array($vendorDir . '/doctrine/instantiator/src/Doctrine/Instantiator'),
    'DeepCopy\' => array($vendorDir . '/myclabs/deep-copy/src/DeepCopy'),
    '' => array($baseDir . '/application/core', $baseDir . '/application/model'),
);

我已经尽可能多地阅读了有关自动加载特征的内容,但我还没有找到答案。

我的命令行PHP版本是5.6.30.

删除 ErrorExceptionHandler.php 中的初始 use SingletonTrait 声明,它将对您有用。

在 class 定义之外使用 use 语句声明特征实际上没有意义。官方 PHP 文档中有关于此的评论,您可以在下面看到并直接阅读 here:

Note that the "use" operator for traits (inside a class) and the "use" operator for namespaces (outside the class) resolve names differently. "use" for namespaces always sees its arguments as absolute (starting at the global namespace)

同样值得注意的是,调整您的 autoload 部分:

{
    autoload": {
        "psr-4": { 
            "": [
                "application/core/",         
                "application/model/"
            ],
           "Utilities\": "application/core/",
        }
    }
}

SingletonTraitFrameworkException 驻留在 Utilities 命名空间中,因此它们已经被 Utilities\ 到 [=18 的映射覆盖=].

同样的原因建议删除 SingletonTrait 的导入(正如 Peter Featherstone 已经回答的那样),因为该名称的特征不存在于根命名空间,但在 Utilities 命名空间内:

<?php

namespace Utilities;

if(!defined('DIRECT_ACCESS')) {
    die("Direct access is forbidden.");
}

class ErrorExceptionHandler implements IUtilities 
{
    use SingletonTrait;

    ....
}

或者,您可以调整 import 语句,但同样,因为我们已经在 Utilities 命名空间中,所以这没有任何意义:

<?php

namespace Utilities;

use Utilities\SingetonTrait;

if(!defined('DIRECT_ACCESS')) {
    die("Direct access is forbidden.");
}

class ErrorExceptionHandler implements IUtilities 
{
    use SingletonTrait;

    ....
}