duandaiqin6080 2016-02-01 20:59
浏览 622

Composer自动加载无法正常工作

I have following problem: I have class Router (in project/connection/api/callbacks) and TestRouter (in project/tests/api).

Class Router is only Example and I don't want psr-0 or 4.

Router has this code on the beginning:

<?php

namespace Connection\Api\Callbacks;

class Router
{

Test class start with this code:

<?php

$loader = require __DIR__ . '/../../vendor/autoload.php';
$loader->add('Connection\\Api\\Callbacks', __DIR__ . '/../../connection/api/callbacks');

class TestRouter extends PHPUnit_Framework_TestCase
{

    function test() {
        $variable = new \Connection\Api\Callbacks\Router();
    }

Then I got error class not found. Please where is the problem?

  • 写回答

1条回答 默认 最新

  • douyousu9691 2016-02-02 22:54
    关注

    You don't want PSR-0, but you are using exactly that function. The code comment for the add() method you are using to add your class to the autoloader:

    /**
      * Registers a set of PSR-0 directories for a given prefix, either
      * appending or prepending to the ones previously set for this prefix.
      *
      * @param string       $prefix  The prefix
      * @param array|string $paths   The PSR-0 root directories
      * @param bool         $prepend Whether to prepend the directories
      */
    public function add($prefix, $paths, $prepend = false)
    

    I wonder what keeps you from simply using PSR-4 in this case. Just add the necessary declaration to your composer.json file.

    When it comes to PHPUnit, add require "vendor/autoload.php" to the bootstrap file or use it as the full bootstrap file if you don't have to do anything else there. It will make writing tests easier because you don't have to take care of the autoloading and adding the individual class to the autoloader. Also, you won't end up instantiating multiple autoloaders that don't get removed from the autoloader stack.

    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧