簡體   English   中英

Composer沒有加載我自己的psr-0文件

[英]Composer is not loading my own psr-0 files

我似乎無法使用psr-0自動加載機制讓作曲家使用我自己的類/文件。 任何人都可以說明為什么以下不起作用?

我在錯誤日志中收到以下輸出:

PHP致命錯誤:第5行/home/webroot/bitlama/index.php中找不到類'TestdirTest1'

它確實有效如果我取消注釋顯式require語句(index.php:2)。

如果有人想知道 - 是的,我已經以'php ../composer.phar install'的形式運行composer install。

這是我的目錄結構:

├── composer.json
├── index.php
├── testspacedir
│   └── Testdir
│       └── test1.php
└── vendor
    ├── autoload.php
    └── composer
        ├── autoload_classmap.php
        ├── autoload_namespaces.php
        ├── autoload_real.php
        └── ClassLoader.php

composer.json:

{
    "autoload": {
        "psr-0": { "Testdir\\": "testspacedir/"}
    }
}

test1.php:

<?php

namespace Testdir;

class Test1 {

    public function __construct()
    {
        echo "Woohoo Test1";
    }

}

index.php文件:

<?php
require 'vendor/autoload.php';
//require 'testspacedir/Testdir/test1.php';

$test1 = new Testdir\Test1();

供應商/ autoload.php:

<?php

// autoload.php @generated by Composer

require_once __DIR__ . '/composer' . '/autoload_real.php';

return ComposerAutoloaderInit7b4760e0f7ca9d6454dd2f098c374ba4::getLoader();

我的類文件被命名為test1.php ,而不是所需要的PSR-0命名約定Test1.php

你說它有效,因為你刪除了require 'testspacedir/Testdir/test1.php'; 這是正確的。

由於您在composer.json中的autoload中定義了命名空間 - >文件夾結構,因此vendor/autoload.php為您處理這些文件夾的加載。

看看那個vendor/autoload.php文件,你會親眼看看。

總結一下,composer會為您處理文件的自動加載,因此您不必執行這些包含。 以下是http://getcomposer.org/doc/01-basic-usage.md#autoloading的片段

注意:Composer提供自己的自動加載器。 如果您不想使用那個,可以只包含vendor / composer / autoload_namespaces.php,它返回一個關聯數組映射名稱空間到目錄。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM