CI_Loader::_ci_load_class() trys to redeclare extended libraries |
|||
|---|---|---|---|
| Date: | 05/02/2007 | Severity: | Major |
| Status: | Resolved | Reporter: | JAAulde |
| Version: | 1.5.3 | ||
| Keywords: | Libraries, Loader Class | ||
Description
When calling load->library() on a core class that has not been extended the loader checks to see if the class has already been instantiated and returns without reloading it. However, if you extend a class, that check is not performed so if an extended library has been loaded somewhere and you try to load it again you get a PHP fatal error for redeclaring a class.
The pertinent code is in system/libraries/Loader.php in the _ci_load_class() method starting on line 676. A conditional should be placed above line 693 checking if the class has already been loaded and returning without further action if it has. The conditional should be almost identical to that found on lines 713-718.
The sample code in this report assumes a MY_Encrypt.php exists in application/libraries/. It is also an overly simplified but accurate representation of the problem—In a real world application it is more likely that two other libraries would be loaded loaded both of which attempt to load ‘encrypt’.
Code Sample
<?php
class Welcome extends Controller {
function Welcome()
{
parent::Controller();
}
function index()
{
$this->load->library('encrypt');
$this->load->library('encrypt');
echo $this->encrypt->sha1('a string');
}
}
?>
Expected Result
555d01e6c83266b3e9f92bd811905370caf62770
Actual Result
PHP Fatal error: Cannot redeclare class ci_encrypt in
Comment on Bug Report
| Posted by: Shadowhand on 2 May 2007 1:34pm | |
|
|
Here’s a patch that will not only fix his bug, but add some additional features (ability to extend any class from BASEPATH or APPPATH, and extend non-system libraries in APPPATH): http://wgilk.com/code/MY_Loader.php JAAlude has tested this for me and reported no issues. |
| Posted by: JAAulde on 2 May 2007 1:39pm | |
|
|
Agreed..looks good! I am going to incorporate this patch into ‘modLoader’ ( http://codeigniter.com/forums/viewthread/49157/ ) as well so that the two are merged if we move forward in that arena. Jim |
| Posted by: JAAulde on 3 May 2007 9:43am | |
|
|
Ok, Woody’s patch as give in the link above works perfectly on the current 1.5.3 code. I have also incorporated it into modLoader and put the new version of that online. |
| Posted by: JAAulde on 3 May 2007 1:36pm | |
|
|
Woody…sophistry’s testing uncovered a PHP5 only function in your patch: [url=http://www.php.net/manual/en/function.str-ireplace.php]str_ireplace()[/url This should be changed back to str_replace() since CIs official stance is full support of PHP4. I have adjusted modLoader already. Jim |
| Posted by: Shadowhand on 3 May 2007 2:16pm | |
|
|
Done. |
| Posted by: AI on 6 May 2007 5:57am | |
|
|
how do i use the patch loacated at |
| Posted by: JAAulde on 6 May 2007 7:00am | |
|
|
Until it is rolled into a fix, you can place Woody’s MY_Loader.php in your application/libraries/ directory. His new functionality should override the existing functionality and fix the problem. |
| Posted by: ammar on 24 May 2007 4:16am | |
|
|
I applied a simple fix: http://codeigniter.com/forums/viewthread/52959/ |
| Posted by: starbbs on 11 August 2007 4:07am | |
|
|
Well this bug is in everty version of CI. It seems that it is too difficult to stop this behaviour. BTW. I still use the load->script(‘script’) function. Its still in the source. When i load a script for the secons time, it just loads |
