Loading namespaced classes with Symfony 1.4's autoloader?

后端 未结 3 1489
独厮守ぢ
独厮守ぢ 2021-02-03 13:07

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

3条回答
  •  礼貌的吻别
    2021-02-03 13:44

    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

提交回复
热议问题