Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible error in JLoader::registerPrefix #7224

Closed
joomdonation opened this issue Jun 21, 2015 · 5 comments
Closed

Possible error in JLoader::registerPrefix #7224

joomdonation opened this issue Jun 21, 2015 · 5 comments

Comments

@joomdonation
Copy link
Contributor

Issue description

Recently, I had issue with auto-load in my custom extension using JLoader::registerPrefix:

I have a MVC library which has prefix OS. OSController, OSModel.... So I register that prefix using this code:

JLoader::registerPrefix('OS', PATH_TO_LIBRARY_FOLDER);

My component has the name com_osmembership and I want my controllers, models, views.... to be auto-loading as well, so I register another prefix:

JLoader::registerPrefix('OSMembership', PATH_TO_MY_COMPONENT);

The problem happens when Joomla tries to load a class in my component, for example OSMembershipController. Joomla sees that this class started with OS, so It tries to find that class from PATH_TO_LIBRARY_FOLDER as starting point for finding. Of course, it could not found the class and result class not found error. As you can see, I expect that Joomla will find that class from PATH_TO_MY_COMPONENT. If Joomla does that, it will find the class and won't return class not found error.

So the question is: Is this a bug ? or I am using it in a wrong way?

Possible solution:

If we agree that this is a bug, I think we can modify the code of _autoload. The idea is looping over all the registered prefix until we found a class. The change code is:

    private static function c($class)
    {
        foreach (self::$prefixes as $prefix => $lookup)
        {
            $chr = strlen($prefix) < strlen($class) ? $class[strlen($prefix)] : 0;
            if (strpos($class, $prefix) === 0 && ($chr === strtoupper($chr)))
            {
                $found =  self::_load(substr($class, strlen($prefix)), $lookup);
                if ($found !== false)
                {
                    return $found;
                }
            }
        }

        return false;
    }

That code solve the issue but will make the system loop few more times, not sure it will cause any performance issue?

@Fedik
Copy link
Member

Fedik commented Jun 21, 2015

try JLoader::registerPrefix('OSMembership', PATH_TO_MY_COMPONENT, false, true);

@joomdonation
Copy link
Contributor Author

That won't work. The $prepend parameter (which you set to true) only has affect if you called JLoader::registerPrefix with the same prefix more than one time.

In the sample I mentioned above, I register two different prefixes. And the first registered prefix is a part of the second prefix

@Fedik
Copy link
Member

Fedik commented Jun 21, 2015

@joomdonation but you tried or you about theory? 😉
in similar cases (prefix is a part of the second prefix) it helps for me, so I thought it will help for you also

@joomdonation
Copy link
Contributor Author

@Fedik Yes, I tried and received the error: Class 'OSMembershipController' not found

@joomdonation
Copy link
Contributor Author

If the longer prefix (OSMembership) is registered before the shorter prefix (OS) then it works. We can control it if the code is from same extension but if it is from two different extensions, it will be difficult. So unless I am wrong, I think this is a bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants