I have the following class autoload definition in a [root]/composer.json
file:
{
...
"autoload": {
"psr-0": {
"": [
"application/models",
"application/controllers",
"application/forms",
"library/"
]
},
"psr-4": {
"": ["src/"]
},
"classmap": [
"app/AppKernel.php",
"app/AppCache.php"
]
},
...
}
When I call the [root]/public_html/index.php
page I got the following error:
PHP Fatal error: Uncaught Error: Class 'classes\DependencyInjection' not found in /var/www/html/application/bootstrap.php:29
What's in [root]/public_html/index.php
is the following code:
$bootstrap = true;
require_once '../application/bootstrap.php';
And what's in [root]/application/bootstrap.php
file is:
// turn on autoloading for classes
// composer autoloader
include(MMIPATH.'/vendor/autoload.php');
// zend autoload
require_once 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::getInstance();
$diContainer = new classes\DependencyInjection(services.yaml');
$proxy = $diContainer->get('containerProxy');
This is the definition of [root]/library/classes/DependencyInjection.php
:
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
USE Symfony\Component\DependencyInjection\Container;
class DependencyInjection extends ContainerBuilder
{
....
}
What's wrong here? Why autoloader can't find that class?
You're attempting to load a "classes" namespace, however your class is not defined as being in the "classes" namespace.
new classes\DependencyInjection(...)
in PSR-0 loads {paths}\classes\DependencyInjection.php
and attempts to instantiate the class DependencyInjection
from the namespace classes
, but DependencyInjection
is not in the classes
namespace. The file will load but the class does not exist.
You could add namespace classes;
to each of these classes, but that's not exactly a good solution. Better solution is to use proper namespacing or change your PSR-0 list to include library/classes and use new DependencyInjection(...)
. (My vote is for the first one -- use proper namespaces.)
As requested. example:
File Location
{app}\library\UsefullNamespace\DependencyInjection.php
Call it using
new UsefullNamespace\DependencyInjection.php
DependencyInjection.php:
namespace UsefullNamespace;
use [...];
class DependencyInjection extends ContainerBuilder
{