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

strange way of using registerPrefixes in loader

I'm trying to split my lib folder into a few folders. I thought that prefixes in autoloader should be the most balanced method between code writing and performance. The problem is that I need to rename every file and every class name inside to match the name with prefix included.

Before:

-app
----lib
-------Class1.php
-------Class2.php
-------Class3.php

After how it must be to work:

-app
----lib
--------general
-----------General_Class1.php
-----------General_Class2.php
--------app
-----------App_Class3.php

After how I want it to be:

-app
----lib
--------general
-----------Class1.php
-----------Class2.php
--------app
-----------Class3.php

Right now to register those prefixes I'm using the following code:

// Register some prefixes
$loader->registerPrefixes(
    array(
        "App_"    => $config->application->libraryDir . "app/",
        "General_"    => $config->application->libraryDir . "general/",
    )
);

$loader->register();

$server = new App_Class3();
echo $server->testMethod('test');

It it possible to call certain classes with for example App_Class3 but without renaming the file and the class name inside (using prefixes method or similar)?



4.9k
edited Aug '15

It tried to but it still doesn't work for me.

// Register some prefixes
$loader->registerNamespaces(
    array(
        "Lib"    => $config->application->libraryDir,
        "Lib\App"    => $config->application->libraryDir . "app/"
    )
);

$loader->register();

$server = new Lib\App\Class3();
echo $server->thestMethod('test');

returns:

Fatal error: Class 'Lib\App\Class3' not found

my libraryDir is: 'libraryDir' => APP_PATH . '/app/library/'



7.9k

try this

$loader->registerNamespaces(
    array(
        "Lib"    => $config->application->libraryDir
    ),
    true
);

also could you please provide namespace in Class3

How to implement HTMLPurifier into project?