We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Phalcon\Loader with composer

call_user_func(function () {
    $composerLoader = require_once __DIR__ . '/vendor/autoload.php';
    $loader = new Phalcon\Loader();

    $loader->registerNamespaces($composerLoader->getPrefixesPsr4(), true);
    $loader->registerNamespaces($composerLoader->getPrefixes(), true);
    $loader->registerClasses($composerLoader->getClassMap(), true);
    $loader->register();
    var_dump($composerLoader->getPrefixesPsr4());
    $loader->autoLoad('WeChat\\Utils');
});

when i execute php _loader.php the output is

array(10) {
  ["WeChat\"]=>
  array(1) {
    [0]=>
    string(39) "/Users/ian/Documents/prolove/src/WeChat"
  }
  ["Symfony\Component\Yaml\"]=>
  array(1) {
    [0]=>
    string(48) "/Users/ian/Documents/prolove/vendor/symfony/yaml"
  }
  ["React\Promise\"]=>
  array(1) {
    [0]=>
    string(53) "/Users/ian/Documents/prolove/vendor/react/promise/src"
  }
  ["Predis\"]=>
  array(1) {
    [0]=>
    string(53) "/Users/ian/Documents/prolove/vendor/predis/predis/src"
  }
  ["Phalcon\"]=>
  array(1) {
    [0]=>
    string(69) "/Users/ian/Documents/prolove/vendor/phalcon/incubator/Library/Phalcon"
  }
  ["GuzzleHttp\Stream\"]=>
  array(1) {
    [0]=>
    string(58) "/Users/ian/Documents/prolove/vendor/guzzlehttp/streams/src"
  }
  ["GuzzleHttp\Ring\"]=>
  array(1) {
    [0]=>
    string(58) "/Users/ian/Documents/prolove/vendor/guzzlehttp/ringphp/src"
  }
  ["Elasticsearch\"]=>
  array(1) {
    [0]=>
    string(81) "/Users/ian/Documents/prolove/vendor/elasticsearch/elasticsearch/src/Elasticsearch"
  }
  ["Doctrine\Instantiator\"]=>
  array(1) {
    [0]=>
    string(83) "/Users/ian/Documents/prolove/vendor/doctrine/instantiator/src/Doctrine/Instantiator"
  }
  ["App\"]=>
  array(1) {
    [0]=>
    string(36) "/Users/ian/Documents/prolove/src/App"
  }
}

Notice: Array to string conversion in /Users/ian/Documents/prolove/_loader.php on line 16

and I found the $loader->registerNamespace accept only

array(
  'namespace'=>'path'
)

not the composer's

array(
  'namespace'=>array('path1', 'path2')
)

Is this a problem?

edited Apr '16

I am using both autoloaders. composer autoloader and Phalcon autoloader, no need to push composer namespaces to Phalcon loader. Just require composer autoloader and make instance of Phalcon loader, where you register application namespaces, thats all you need.

You can use both loaders. Just load your classes which extends phalcon using phalcon autoloader and from some composer packages use composer loaders.

edited Apr '16

Agree, i did it that way. autoload of composer is a plus. all you need to do is just include it.