Question

I'm trying to setup symfony class loader component for one of my projects. When I try to create a new object from the Logger class I get

Fatal error: Class 'MyPrefix\Log\Logger' not found in /usr/htdocs/sf/index.php on line 12

Here is the structure of the project

/
 lib
    MyPrefix
      Log
        Logger.php
 vendor/
 index.php

Here is the content of index.php file

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

use Symfony\Component\ClassLoader\ClassLoader;

$loader = new ClassLoader();
$loader->addPrefix('MyPrefix', __DIR__ .'/lib/MyPrefix/');
$loader->register();


use MyPrefix\Log\Logger;
$logger = new Logger();

What I'm doing wrong?

Was it helpful?

Solution

When you add new prefix for namespace, you should give parent directory.

So for MyPrefix it is __DIR__ . '/lib/'

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top