I'm trying to use my custom namespace for my personal classes.
The directory structure is (as usual):
my_project/ - src/ |- myComponent.class.php \- myWrapper.class.php - vendor |- OtherLibrary \- Symfony - composer.json - index.php
in my composer.json I specify my own namespace with:
"autoload": {
"psr-0": {
"my_namespace\\": "src/"
}
}`
then in my PHP code I have something like:
namespace my_namespace;
class myComponent
{
.... code
}
namespace my_namespace;
require_once __DIR__.'/vendor/autoload.php';
$component = new myComponent();
Fatal error: Class 'my_namespace\myComponent' not found in /path_to_root/my_project/index.php on line 5
I would expect myComponent to be searched under my_project/src/, as specified in the composer.json and as defined into vendor/composer/autoload_namespaces.php ('my_namespace\\' => array($baseDir . '/src')
).
I would expect to directly call my custom myComponent, when I define the namespace to my own namespace. Am I wrong?
What's wrong in my code and my assumptions? How should I fix it?
You found the errors yourself, but here is a quick collection of what the useful autoload directives in Composer do:
composer.json
file. A class myNamespace\myClass
and "psr-0":{"myNamespace\\": "src"}
will try to load src/myNamespace/myClass.php
.composer.json
from the full class name, and the remainder is converted into a path, ".php" added at the end, and searched in the path given. A class myNamespace\myClass
and "psr-4":{"myNamespace\\": "src"}
will try to load src/myClass.php
.