I have created a simple package on packagist for learning (tommytest/tommytest). It installs fine, but I have to run "composer dump-autoload -o" immediately after it is installed. Before manually running the dump-autoload I get this:
Fatal error: Class 'mysqlgridspace\Page' not found in C:\xampp\htdocs\simple\index.php on line 5
After manually running dump-autoload it works fine. Isn't it supposed to handle the autoload set-up automatically when the package is installed?
Here's my composer.json (located in: simple/vendor/tommytest/tommytest/composer.json)
{
"name": "tommytest/tommytest",
"type": "library",
"description": "Framework agnostic data grid",
"keywords": ["datagrid","data grid"],
"homepage": "https://github.com/escalibore/tommytest",
"license": "MIT",
"authors": [
{
"name": "Tommy Bert",
"email": "[email protected]",
"homepage": "http://tberthold.com",
"role": "Developer"
}
],
"require": {
"php": ">=5.3.0"
},
"autoload": {
"psr-4": {
"mysqlgridspace\\":"src/"
}
}
}
And my class file (located in: simple/vendor/tommytest/tommytest/src/Mysqlgridmain.class.php)
<?php
namespace mysqlgridspace;
class Page {
public function __construct()
{
echo "hello, i am a page.<br>";
}
}
class Book {
public function __construct()
{
echo "hello, i am a book.<br>";
}
}
Those classes cannot be autoloaded with Composer's PSR-4 autoloader because it resolves the class name mysqlgridspace\Page
to a file named Page.php
, which should exist in src
- it doesn't, though.
First of all, there should only be one class declared in each file.
You should have
src/
Book.php
Page.php
each declaring one class only.
Then your PSR-4 autoloading should work.
For reference, see http://www.php-fig.org/psr/psr-4/.