1
votes

I am trying to create Hello world package with composer from this tutorial

http://grossi.io/2013/creating-your-first-composer-packagist-package/

Somehow I have managed to create it and test half day ago, but now in diferent directory I have tried again few times - cannot make it work and do not understand why I get error which I will show later.

Here is what I do:

In HelloWorld directory I create src directory. In src directory I put file SayHello.php

namespace HelloWorld;

class SayHello
{
    public static function world()
    {
        return 'Hello World, Composer!';
    }
}

Run

composer init

After it creates composer.json file, I edit and in the end it is such:

{
    "name": "vagrant/hello-world",
    "description": "test",
    "license": "no",
    "authors": [
        {
            "name": "darius",
            "email": "[email protected]"
        }
    ],
    "minimum-stability": "dev",
    "require": {
        "php": ">=5.3.0"
    },
    "autoload": {
        "psr-0": {
            "HelloWorld": "src/"
        }
    }
}

Then I run

composer install

I get output:

Loading composer repositories with package information
Installing dependencies (including require-dev)
Nothing to install or update
Generating autoload files

Tutorial says

Composer installed create a directory "tests" inside your root dir.

But I don't see such directory, so I create it myself in the HelloWorld directory. Maybe the author missed to add something.

Then I create file test.php

require_once __DIR__ . '/../vendor/autoload.php'; // Autoload files using Composer autoload

use HelloWorld\SayHello;

echo SayHello::world();

Run:

php tests/test.php

And get:

PHP Fatal error: Class 'HelloWorld\SayHello' not found in /home/vagrant/package_dev/workbench/oitlabs/HelloWorld/tests/test.php on line 7

How can it now see it? I tried ading to test.php

require_once __DIR__ . '/../src/SayHello.php';

then it see and works, so it means it should see the file. So its kind of like composer generates wrong autoload file or something. How can I debug this?

Also tried same steps in another directory near the one I first time succeeded, just using namespace Hello. Same error.

Here is also the file - how my package looks, what is generated by composer: http://www58.zippyshare.com/v/Hsfg4pVf/file.html

1

1 Answers

1
votes

The problem was - I needed to have directory "HelloWorld" in the src directory.

So the folder structure would look like this:

Root
  src
      HelloWorld
            SayHello.php
  tests
      test.php
  vendor
  composer.json