Composer Autoloading
Asked Answered
H

5

10

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

My directory structure looks like this

- Project
    - src
        - Twitter
            Twitter.php
    - vendor
    - Test
    index.php

My index.php file looks like this:

<?php

    use Twitter;
    $twitter = new Twitter();

My Twitter.php file looks like this

<?php 

namespace Twitter;

class Twitter
{
    public function __construct()
    {
        // Code Here
    }
}

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"
    }
}
}

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?

Hartz answered 24/11, 2013 at 21:21 Comment(1)
You will also have to include the autoloader.php file - but then, if everything else is defined in the composer.json file, that may well be the only thing you explicitly load with a require/include. Se the Composer FAQs for more details how to use it.Swam
G
11

In your composer.json use:

"autoload": {
    "psr-0": {
        "": "src/"
    }
}

or

"autoload": {
    "psr-0": {
        "Twitter\\": "src/"
    }
}

and then run php composer.phar dump-autoload

Gehring answered 24/11, 2013 at 21:26 Comment(0)
A
3

Use

"psr-0": {
     "Twitter" : "src/"
 }

This instructs composer to create autoloader, that will look in src for everything from Twitter namespace. And since it is PSR-0, namespace is treated as a folder and added to declared path, so you should not include it in path part in composer.json

Astrosphere answered 24/11, 2013 at 21:26 Comment(5)
"so you should not include it composer.json" what do you mean by this?Hartz
Missed few words, you should not include it in path partAstrosphere
I'm still getting the same error. Is there any information online that explains how to do this? Are the rest of my files set up correctly?Hartz
I forgot to add ... require 'vendor/autoload.php';Hartz
As for documentation, sure there is - getcomposer.org/doc/04-schema.md#autoloadAstrosphere
P
2

First of all,

My index.php file looks like this:

use Twitter;
$twitter = new Twitter();

If it's your index.php you forgot to include the composer's autoload script first.

require __DIR__ . '/vendor/autoload.php';

See https://getcomposer.org/doc/01-basic-usage.md#autoloading for details.

Pentad answered 6/2, 2016 at 12:13 Comment(0)
T
1

There is an error in your index.php, should be: use Twitter\Twitter; $twitter = new Twitter(); or $twitter = new Twitter\Twitter();

Tertian answered 17/8, 2014 at 15:13 Comment(0)
C
0

This is a very late reply but the first thing you need to make "autoloading" works is have your PHP version be 5.6 and above.

Coffeehouse answered 23/4, 2017 at 8:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.