Part of the EllisLab Network
   
 
Bug Avoidance—> Loader::_ci_init_class()
Posted: 26 March 2007 04:10 AM   [ Ignore ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1749
Joined  06-23-2006

I’d like to see a simple code correction in the following method. You can see that the class has a default $config parameter initialized to FALSE, but the method itself assumes $config is an Array if it is !== NULL. That can introduce some bugs in libraries when $config is passed in as FALSE and not an array.

$config should default to NULL or an empty array.

/**
* Instantiates a class
*
* @access    private
* @param    string
* @param    string
* @return    null
*/
function _ci_init_class($class, $prefix = '', $config = FALSE)
{    
    
// Is there an associated config file for this class?
    
if ($config === NULL)
    
{
        $config
= NULL;
        if (
file_exists(APPPATH.'config/'.$class.EXT))
        
{
            
include(APPPATH.'config/'.$class.EXT);
        
}
    }
    
    
if ($prefix == '')
    
{
        $name
= (class_exists('CI_'.$class)) ? 'CI_'.$class : $class;
    
}
    
else
    
{
        $name
= $prefix.$class;
    
}
    
    
// Set the variable name we will assign the class to
    
$class = strtolower($class);            
    
$classvar = ( ! isset($this->_ci_varmap[$class])) ? $class : $this->_ci_varmap[$class];
            
    
// Instantiate the class        
    
$CI =& get_instance();
    if (
$config !== NULL)
    
{
        $CI
->$classvar = new $name($config);
    
}
    
else
    
{        
        $CI
->$classvar = new $name;
    
}    
}
 Signature 

Mac OS X 10.4.10, Apache 1.3.3, PHP 5.2.3, CodeIgniter 1.5.x., baby!

Profile
 
 
Posted: 26 March 2007 10:30 AM   [ Ignore ]   [ # 1 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  351
Joined  07-25-2006

Using boolean false with == is the correct way to do it, because:

$arr = array();
if (
$arr == false) {// Evaluates true
  
die ('$arr is empty');
}

if ($arr == null) {// Also evaluates true
  
die ('$arr is empty');
}
 Signature 

me and some random code, hosted by dh. and a blog too! ++ dead bugs

Profile
 
 
Posted: 26 March 2007 11:33 AM   [ Ignore ]   [ # 2 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  737
Joined  10-18-2006
Shadowhand - 26 March 2007 10:30 AM

Using boolean false with == is the correct way to do it, because:

$arr = array();
if (
$arr == false) {// Evaluates true
  
die ('$arr is empty');
}

if ($arr == null) {// Also evaluates true
  
die ('$arr is empty');
}

I don’t think so… you might want to pass an empty array as a config and it will be overwritten by config file, but you are assigning the config values.

 Signature 

Once in a while I remember I use Twitter

Profile
 
 
Posted: 26 March 2007 12:58 PM   [ Ignore ]   [ # 3 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  351
Joined  07-25-2006

Ah, I see what you are saying Seppo. I misinterpreted what you were reporting here. I’ll have to think about this one some more.

 Signature 

me and some random code, hosted by dh. and a blog too! ++ dead bugs

Profile
 
 
Posted: 26 March 2007 12:59 PM   [ Ignore ]   [ # 4 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1749
Joined  06-23-2006
Shadowhand - 26 March 2007 10:30 AM

Using boolean false with == is the correct way to do it, because:

The issue at hand here is that $config passed into libraries should be an array, since that’s the design of CI’s configuration files. Each library shouldn’t have to check whether or not that the value passed in is a valid array or not. The Loader should handle that for each library, either passing in an array or nothing, at which point the library’s default parameter value gets passed.

function MY_Library($config = array()) {

}

That’s my take anyway.

 Signature 

Mac OS X 10.4.10, Apache 1.3.3, PHP 5.2.3, CodeIgniter 1.5.x., baby!

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 819, on March 11, 2010 11:15 AM
Total Registered Members: 120464 Total Logged-in Users: 38
Total Topics: 126542 Total Anonymous Users: 2
Total Replies: 665377 Total Guests: 315
Total Posts: 791919    
Members ( View Memberlist )