drmeu26880 2014-06-29 22:56
浏览 55
已采纳

包自动加载问题

I'm struggling to work out how to correctly build and autoload my first composer package. I understand that if I follow the PSR-0 naming convention, autoloading should "just work". I am trying to set up with:

  • vendor: Programster
  • package name: CoreLibs

I have managed to debug a project that includes the package to the point where I am outputting the search path by putting a print in the findFileWithExtension() function of the ClassLoader.php file

/home/stuart/Desktop/test-project/vendor/Programster/CoreLibs/Programster/Programster/CoreLibs/Core.php

I know that the file is actually located at:

/home/stuart/Desktop/test-project/vendor/Programster/CoreLibs/Core.php

My Package's composer.json file:

{
    "name": "Programster/CoreLibs",
    "type": "library",
    "description": "Core libraries for PHP 5.3+",
    "keywords": ["core","library"],
    "homepage": "http://svn.yadda-yadda.com/php/core-libs",
    "license": "none",
    "authors": [
        {
            "name": "xxxx",
            "email": "my.email@email.com",
            "homepage": "http://mywebsite.com/",
            "role": "Developer"
        }
    ],
    "require": {
        "php": ">=5.3.0"
    },
    "autoload": {
        "psr-0": {
            "Programster": "Programster"
        }
    }
}

The following is a tiny script in a project that includes the package to test if is working:

<?php

require_once(__DIR__ . '/../vendor/autoload.php');

$loader = new \Composer\Autoload\ClassLoader();
$loader->register();

use \Programster\CoreLibs as programster;
programster\Core::println("hello world");

The project's composer.json file to include the package:

{
    "repositories": [ { "type": "composer", "url": "http://satis.mydomain.com/" } ],
    "require": {
        "Programster/CoreLibs": "dev-trunk"
    }
}

Is my package's config wrong, or do I really need to create Programster/CoreLibs/ subdirectories within the CoreLibs repository source code?

  • 写回答

2条回答 默认 最新

  • duanci6484 2014-06-30 10:20
    关注

    I managed to get it working by using the PSR-4 standard that others suggested in the comments and updating my package's composer.json file (see the autoload section):

    {
        "name": "Programster/CoreLibs",
        "type": "library",
        "description": "Core libraries for PHP 5.3+",
        "keywords": ["core","library"],
        "homepage": "http://svn.mydomain/core-libs",
        "license": "none",
        "authors": [
            {
                "name": "MY name",
                "email": "my.email@email.com",
                "homepage": "http://my-website.com/",
                "role": "Developer"
            }
        ],
        "require": {
            "php": ">=5.3.0"
        },
        "autoload": {
            "psr-4": {
                "Programster\\CoreLibs\\": ""
            }
        }
    }
    

    This is my script in the project that has the package installed:

    require_once(__DIR__ . '/../vendor/autoload.php');
    \Programster\CoreLibs\Core::println("hello world");
    

    @sectus was right, I did not need to use $loader = new \Composer\Autoload\ClassLoader();$loader->register();

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

报告相同问题?

悬赏问题

  • ¥20 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏