8

How to register namespaces (with PHP 5.3) in the Symfony 1.4 for the autoloader class feature (like the Symfony 2.0)?

hakre
  • 193,403
  • 52
  • 435
  • 836
Wagner Pinheiro
  • 339
  • 5
  • 14
  • Were you able to get Symfony 1.4 autoloading working with namespaces finally? If so, can you let me know how? – Eric B. Dec 19 '12 at 03:00

3 Answers3

10

You can use Autoloader from Symfony2 in Symfony 1.4 framework.

1. Copy Symfony2 classloaders to vendor directory of your Symfony 1.4 sandbox project:

SF_ROOT_DIR/lib/vendor/Symfony2/src/Symfony/Component/ClassLoader/UniversalClassLoader.php

SF_ROOT_DIR/lib/vendor/Symfony2/src/Symfony/Component/ClassLoader/ApcUniversalClassLoader.php

2. Modify your SF_ROOT_DIR/config/ProjectConfiguration.class.php file as follows:

require_once dirname(__FILE__) . '/../lib/vendor/symfony/lib/autoload/sfCoreAutoload.class.php';
require_once dirname(__FILE__) . '/../lib/autoload/sfClassLoader.class.php';
sfCoreAutoload::register();

class ProjectConfiguration extends sfProjectConfiguration {

    public function setup() {
        $this->namespacesClassLoader();
        $this->enablePlugins('sfDoctrinePlugin');
    }

    public function namespacesClassLoader() {
       if (extension_loaded('apc')) {
           $loader = new ApcUniversalClassLoader('S2A');
       } else {
           $loader = new UniversalClassLoader();
       }
       $loader->registerNamespaces(array(
          'Pohon' => __DIR__ . '/../lib/vendor/Pohon/src'));
       $loader->register();
    }

}

3. Register desired namespaces:
eg. I want to load class:

Pohon\Tools\String\Utils\Slugify.

Filename must be:

SF_ROOT_DIR/lib/vendor/Pohon/src/Pohon/Tools/String/Utils/Slugify.php

and registered namespace as follows:

Pohon => SF_ROOT_DIR/lib/vendor/Pohon/src

Jan Míšek
  • 1,647
  • 1
  • 16
  • 22
  • The Autoloader project can be found here: https://github.com/Symfony-Plugins/sfClassLoaderPlugin. – Eric B. Dec 21 '12 at 02:26
  • Breautiful, thanks! I had to add `use Symfony\Component\ClassLoader\UniversalClassLoader; use Symfony\Component\ClassLoader\ApcUniversalClassLoader;` after the imports in the ProjectConfiguraration.class.php file. Also, here's the link for the ClassLoader bundle (before these classes got deprecated): https://github.com/symfony/ClassLoader/tree/2.6 – GermanK Mar 29 '15 at 17:58
5

You can use Composer and it's very easy. Just install it on your machine (you probably have already since it's 2015 now) and run in your project folder:

composer init 

You can then install all the packages you want with composer and include just this line in your ProjectConfiguration.class.php:

require_once __DIR__.'/../vendor/autoload.php';

Note that paths may differ if you changed the default Symfony1.4 directory structure.

ggioffreda
  • 650
  • 6
  • 11
  • Symfony 1.4 don't know how to load classes within a namespace, you must implement this feature. Composer is just a package/lib manager. – Wagner Pinheiro Apr 09 '15 at 23:33
  • 2
    Composer does provide a ClassLoader as well and when you install (to use Composer slang "require") packages with it all you have to do is then include the `vendor/autoload.php` script above. Composer takes care of mapping the namespaces in the installed packages, all of them. Have you tried it? I do that and it works quite nicely. – ggioffreda Apr 10 '15 at 01:45
  • Thank you for this solution it worked for me. you need to specify vendor-dir: "lib/vendor" to have libraries in the original vendor dir of sf 1.4; used that for amp-library and it works fine. – Naeh Aug 10 '16 at 11:54
1

Symfony uses the spl_autoload_register() function to register its own autoloader (sfAutoload).

You can register your own handler in the initialize() function of your Project/Application/Plugin. (whichever applies).

This is, for example, also what the Swift_Mailer plugin does: it registers its own autoloader when needed.

Grad van Horck
  • 4,488
  • 1
  • 21
  • 22