0

I want to use this php library with namespaced classes in my Symfony 1.4 project: https://github.com/donquixote/cellbrush.

I'm not quite familiar with the namespaces concept. So when i fisrt try the to use the main class of this library, according to its docs, i just did:

$table = \Donquixote\Cellbrush\Table\Table::create();

And i got this fatal error:

Fatal error: Class 'Donquixote\Cellbrush\Table\Table' not found in D:\SF_ROOT_DIR\apps\frontend\modules\home\actions\actions.class.php

So i searched for a solution, and supposedly there is one: stackoverflow sol 1, stackoverflow sol 1 eg, but when i try to implement it i still get the above error.

My case:

Directories and files of interest:

D:\SF_ROOT_DIR\lib\autoload\sfClassLoader.class.php

D:\SF_ROOT_DIR\lib\vendor\ClassLoader (contains: https://github.com/symfony/ClassLoader/tree/2.6)

D:\SF_ROOT_DIR\lib\vendor\cellbrush-1.0 (contains: https://github.com/donquixote/cellbrush.)

Code:

SF_ROOT_DIR/config/ProjectConfiguration.class.php

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

use Symfony\Component\ClassLoader\UniversalClassLoader; 
use Symfony\Component\ClassLoader\ApcUniversalClassLoader;

sfCoreAutoload::register();

class ProjectConfiguration extends sfProjectConfiguration
{
  public function setup()
  {
    $this->namespacesClassLoader();
    $this->enableAllPluginsExcept('sfPropelPlugin');
  }

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

actions.class.php

$table = \Donquixote\Cellbrush\Table\Table::create();

Thanks.

Community
  • 1
  • 1
Alvarob
  • 79
  • 1
  • 6

1 Answers1

1

Use composer and its autoloading.

Execute:

composer require donquixote/cellbrush

Now the library is installed in vendor directory and autoloader is generated, you just need to include it. Add this line to the top of config/ProjectConfiguration.class.php:

require_once dirname(__FILE__).'/../vendor/autoload.php';
Marek
  • 7,337
  • 1
  • 22
  • 33
  • Thank you @Marek, the alternative you proposed worked just fine. I had never used composer before so it took me a while to installed. Two things to add: first, the required really is: `require_once dirname(__FILE__).'/../lib/vendor/autoload.php';`, and second for thouse looking to use cellbrush library have in mind that sadly requieres PHP 4 or higher. Strictly, this isn't a precise solution for the problem i pose, but it does for the bigger one, so is a perfect alternative and i recommend it, hope somone else can help with the other alternative. – Alvarob May 20 '15 at 17:41