Composer does not load my own psr-0 files

I can not get the composer to work with my own classes / files using the psr-0 autoload mechanism. Can anyone shed some light on why the following does not work?

I get the following output in my error log:

PHP Fatal error: Class 'TestdirTest1' not found in /home/webroot/bitlama/index.php on line 5

It works if I uncomment the explicit request of require (index.php: 2).

And if someone asks a question - yes, I started the installation of the composer in the form: "php ../ composer.phar install".

This is my directory structure:

├── composer.json ├── index.php ├── testspacedir │  └── Testdir │  └── test1.php └── vendor ├── autoload.php └── composer ├── autoload_classmap.php ├── autoload_namespaces.php ├── autoload_real.php └── ClassLoader.php 

composer.json:

 { "autoload": { "psr-0": { "Testdir\\": "testspacedir/"} } } 

test1.php:

 <?php namespace Testdir; class Test1 { public function __construct() { echo "Woohoo Test1"; } } 

index.php:

 <?php require 'vendor/autoload.php'; //require 'testspacedir/Testdir/test1.php'; $test1 = new Testdir\Test1(); 

seller / autoload.php:

 <?php // autoload.php @generated by Composer require_once __DIR__ . '/composer' . '/autoload_real.php'; return ComposerAutoloaderInit7b4760e0f7ca9d6454dd2f098c374ba4::getLoader(); 
+6
source share
2 answers

My class file was named test1.php instead of the required PSR-0 test1.php .

+3
source

You say this works because you removed require 'testspacedir/Testdir/test1.php'; , and it is right.

Since you defined the namespace structure → in the autoload structure in composer.json , vendor/autoload.php handles loading these folders for you.

Take a look inside this vendor/autoload.php file and you will see for yourself.

To summarize, the composer handles the autoload of files for you, so you do not need to execute them. Here is a snippet from http://getcomposer.org/doc/01-basic-usage.md#autoloading

Note. Composer provides its autoloader. If you do not want to use this, you can simply enable vendor / composer / autoload_namespaces.php, which returns an associative array that maps namespaces to directories.

0
source

All Articles