使用PSR-4时,无法使用Composer自动加载

时间:2019-06-11 18:18:48

标签: php namespaces composer-php autoload

使用psr-4自动加载时,我在运行函数时遇到问题。

当我直接声明应该在自动加载中的文件时,一切正常。当我切换到psr-4时,我得到该功能未定义的日志。

src / cli.php:

namespace Php\Project1\Cli;

function run()
{
  ...
}

bin / brain-games:

#!/usr/bin/env php

<?php

$autoloadPath1 = __DIR__ . '/../../../autoload.php';
$autoloadPath2 = __DIR__ . '/../vendor/autoload.php';

if (file_exists($autoloadPath1)) {
    require_once $autoloadPath1;
} else {
    require_once $autoloadPath2;
}

use function Php\Project1\Cli\run;
run();

composer.json:

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

我得到的错误是:

PHP Fatal error:  Uncaught Error: Call to undefined function Php\Project1\Cli\run() in /home/arkadiy/php-project1/bin/brain-games:15
Stack trace:
#0 {main}
  thrown in /home/arkadiy/php-project1/bin/brain-games on line 15

0 个答案:

没有答案
相关问题