문제

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?

도움이 되었습니까?

해결책

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

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top