作曲家自动加载 [英] Composer Autoloading
本文介绍了作曲家自动加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前正在尝试对Composer使用PSR-0自动加载,但是出现以下错误:
I'm currently trying to use PSR-0 autoloading with Composer, but I'm getting the following error:
Fatal error: Class 'Twitter\Twitter' not found
我的目录结构如下
- Project
- src
- Twitter
Twitter.php
- vendor
- Test
index.php
我的index.php文件如下所示:
My index.php file looks like this:
<?php
use Twitter;
$twitter = new Twitter();
我的Twitter.php文件如下:
My Twitter.php file looks like this
<?php
namespace Twitter;
class Twitter
{
public function __construct()
{
// Code Here
}
}
最后我的composer.json看起来像这样:
And finally my composer.json looks like this:
{
"require": {
"phpunit/phpunit": "3.8.*@dev",
"guzzle/guzzle": "3.7.*@dev"
},
"minimum-stability": "dev",
"autoload": {
"psr-0": {
"Twitter" : "src/Twitter"
}
}
}
我有点困惑。我来自C#背景,这种工作方式使我感到困惑。使用PSR-0自动加载的正确方法是什么?
I am getting a little confused. I come from a C# background and this way of working is kinda confusing me. What's the correct way to use PSR-0 autoloading?
推荐答案
在composer.json中使用:
In your composer.json use:
"autoload": {
"psr-0": {
"": "src/"
}
}
或
"autoload": {
"psr-0": {
"Twitter\\": "src/"
}
}
然后运行 php composer.phar dump-autoload
这篇关于作曲家自动加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文