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?
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?
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
@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
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.
Category | ⇒ | Libraries |
Labels |
Added:
?
|
Status | New | ⇒ | Closed |
Closed_Date | 0000-00-00 00:00:00 | ⇒ | 2016-02-15 14:24:40 |
Closed_By | ⇒ | joomdonation |
try
JLoader::registerPrefix('OSMembership', PATH_TO_MY_COMPONENT, false, true);