dongye9991 2015-12-21 18:31
浏览 43
已采纳

为什么我必须在安装我的包后运行Composer转储?

I have created a simple package on packagist for learning (tommytest/tommytest). It installs fine, but I have to run "composer dump-autoload -o" immediately after it is installed. Before manually running the dump-autoload I get this:

Fatal error: Class 'mysqlgridspace\Page' not found in C:\xampp\htdocs\simple\index.php on line 5

After manually running dump-autoload it works fine. Isn't it supposed to handle the autoload set-up automatically when the package is installed?

Here's my composer.json (located in: simple/vendor/tommytest/tommytest/composer.json)

 {
  "name": "tommytest/tommytest",
  "type": "library",
  "description": "Framework agnostic data grid",
  "keywords": ["datagrid","data grid"],
  "homepage": "https://github.com/escalibore/tommytest",
  "license": "MIT",
  "authors": [
    {
      "name": "Tommy Bert",
      "email": "tom@tberthold.com",
      "homepage": "http://tberthold.com",
      "role": "Developer"
    }
  ],
  "require": {
    "php": ">=5.3.0"
  },
  "autoload": {
    "psr-4": {
      "mysqlgridspace\\":"src/"
    }
  }
}

And my class file (located in: simple/vendor/tommytest/tommytest/src/Mysqlgridmain.class.php)

<?php
namespace mysqlgridspace;

class Page {
    public function __construct()
    {
        echo "hello, i am a page.<br>";
    }
}

class Book {
    public function __construct()
    {
    echo "hello, i am a book.<br>";
    }
}
  • 写回答

2条回答 默认 最新

  • dongyixun0634 2015-12-25 15:07
    关注

    Those classes cannot be autoloaded with Composer's PSR-4 autoloader because it resolves the class name mysqlgridspace\Page to a file named Page.php, which should exist in src - it doesn't, though.

    First of all, there should only be one class declared in each file.

    You should have

    src/
        Book.php
        Page.php
    

    each declaring one class only.

    Then your PSR-4 autoloading should work.

    For reference, see http://www.php-fig.org/psr/psr-4/.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码