0
votes

I want Implemente the namespace autoloading with composer and PSR-0, and I don't know why it dosen't work.

there is my file structure:

src
   |app
       | world 
              | World.php
      
       | user
             | User.php
vendor
Test.php
composer.json

in World.php

<?php

namespace world;
class World {
    public function hello() {
        return "hello world";
    }
}
?>

in User.php

<?php
namespace user; 
class User {
    public function hello() {
        return "hello user";
    }
}
?>

in composer.json

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

and when I test this in Test.php :

<?php
require 'vendor/autoload.php';

class Myworld {
    public function testhello() {
        $w = new my\librairie\World();
        echo $w->hello();
        $u = new my\user\User();
        echo $u->hello();
    }
}

$m = new Myworld();
$m->testhello();
?>

I get this error :

Fatal error: Class 'my\user\User' not found

Fatal error: Class 'my\world\World' not found

what I miss !? Any advice would be welcome! thanks.

1

1 Answers

2
votes

There is no namespace part "my" in your definitions.

namespace user; 
class User {...}

This class is named \user\User, not \my\user\User.

The same applies to \world\World.

Consequently the namespace definition in Composer is wrong. You'd need two definitions for both user and world, both in the same directory:

{
    "autoload": {
        "psr-0": {
            "user\\": "src/app",
            "world\\": "src/app"
        }
    }
}