user1271518
user1271518

Reputation: 636

Symfony class loader - usage? no examples of actual usage

I've been playing a bit with the Symfony class loader (read about others, the concept and started implementing).

I've read http://symfony.com/doc/current/components/class_loader.html as well I've changed the directories structure to fit and all. Here's a small source-code which fails:

Filename: test.php , dir: C:/test/BE/src/main.php

   <?php
    define('BASE_DIR','/../..');

    echo BASE_DIR.'/BE/libs/symphony/component/ClassLoader/2.1.0/UniversalClassLoader.php';
    require_once BASE_DIR.'/BE/libs/symphony/component/ClassLoader/2.1.0/UniversalClassLoader.php';

    use Symfony\Component\ClassLoader\UniversalClassLoader;

    $loader = new UniversalClassLoader();
    $loader->registerNamespace('App\Location\Location', 'Location/Location');

     // You can search the include_path as a last resort.
    $loader->useIncludePath(true);    
    $loader->register();
    use App\Location\Location;
    new App\Location\Location(); //Fatal error: Class 'App\Location\Location' not found in C:/test/BE/src/main.php

file name: Location.php , dir: C:/test/BE/src/Location/Location.php

namespace App\Location;

class Location{
    private $lat;
    private $lng;
}

Upvotes: 4

Views: 5507

Answers (2)

hakre
hakre

Reputation: 197787

You use:

$loader->registerNamespace('Pimple', BASE_DIR.'/BE/libs/Pimple/1.0.0/lib/Pimple');

To register the Pimple namespace for Pimple is useless, as Pimple is one class in the default namespace (it has no namespace).

The UniversalClassLoader does only work for PSR-0 and PEAR compatible directory layouts. If your layout is not, you should not expect it to work nor to be documented that it works for incompatbile layouts.

Explicit Note: You are using the wrong tool to do the job. All file layouts you have shared over the last hours are incompatible with UniversalClassLoader.

Apart from that if you give the wrong values for it's parameters, the class just won't work.

Upvotes: 2

Jakub Zalas
Jakub Zalas

Reputation: 36201

By registering your namespace as follows:

$loader->registerNamespace('App\Location\Location', 'Location/Location');

autoloader will look for the App\Location\Location class in the Location/Location/App/Location/Location.php file. Note that file/directory names are case sensitive.

First parameter of registerNamespace() is either a namespace or part of the namespace. Second parameter is a path to a PSR-0 compliant library. That means that directory structure inside that path should follow PSR-0 standard. This path is not used when calculating path for given namespace.

If you want to keep your current directory structure following code should work for you:

$loader->registerNamespace('App', __DIR__);

I'd rather put your source code in one common directory, for example src:

src/
  App/
    Location/
        Location.php

And then register namespaces as follows:

$loader->registerNamespace('App', 'src');

By doing that you're simply telling an autoloader that it should look for App namespaced classes in the src folder. Note that it will work for every class in the App namespace (following PSR-0).

Some time ago I wrote a blog post about the Symfony autoloader. You might find it helpful: Autoloading classes in an any PHP project with Symfony2 ClassLoader component

Since you plan to use Pimple and some of the Symfony components take a look at Silex microframework. Might be it's something you need to implement.

Upvotes: 11

Related Questions