Part of the EllisLab Network
   
 
Customizing Helpers
Posted: 13 December 2006 12:29 PM   [ Ignore ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  486
Joined  09-14-2006

It would be nice if you easily customize helpers the way you can with the classes. Right now, if you want to add a function or change the functionality of a helper function you have to copy the entire helper file to your application/helpers directory and make your changes there. When your change is small, this seems a little unnecessary to have to copy all the unrelated functions to your new file. If the helper files were all updated, wrapping each function in an if ( ! function_exists()) and the Loader class was updated to load both the original helper and then one the the application/helpers directory, then you could add new functions or custom versions of the existing functions without duplicating all the other code. See below.


The current method in the Loader class for helpers:

function helper($helpers = array())
    
{
        
if ( ! is_array($helpers))
        
{
            $helpers
= array($helpers);
        
}
    
        
foreach ($helpers as $helper)
        
{        
            $helper
= strtolower(str_replace(EXT, '', str_replace('_helper', '', $helper)).'_helper');
        
            if (isset(
$this->_ci_helpers[$helper]))
            
{
                
continue;
            
}

            
if (file_exists(APPPATH.'helpers/'.$helper.EXT))
            
{
                
include_once(APPPATH.'helpers/'.$helper.EXT);
            
}
            
else
            
{        
                
if (file_exists(BASEPATH.'helpers/'.$helper.EXT))
                
{
                    
include(BASEPATH.'helpers/'.$helper.EXT);
                
}
                
else
                
{
                    show_error
('Unable to load the requested file: helpers/'.$helper.EXT);
                
}
            }

            $this
->_ci_helpers[$helper] = TRUE;
            
        
}
        
        log_message
('debug', 'Helpers loaded: '.implode(', ', $helpers));
    
}


My proposed new helper load method:

function helper($helpers = array())
    
{
        
if ( ! is_array($helpers))
        
{
            $helpers
= array($helpers);
        
}
    
        
foreach ($helpers as $helper)
        
{        
            $helper
= strtolower(str_replace(EXT, '', str_replace('_helper', '', $helper)).'_helper');
        
            if (isset(
$this->_ci_helpers[$helper]))
            
{
                
continue;
            
}

            
if (file_exists(APPPATH.'helpers/'.$helper.EXT))
            
{
                
include_once(APPPATH.'helpers/'.$helper.EXT);
                
$this->_ci_helpers[$helper] = TRUE;
            
}
            
if (file_exists(BASEPATH.'helpers/'.$helper.EXT))
            
{
                
include(BASEPATH.'helpers/'.$helper.EXT);
                
$this->_ci_helpers[$helper] = TRUE;
            
}
            
if ( ! isset($this->_ci_helpers[$helper]))
            
{
                show_error
('Unable to load the requested file: helpers/'.$helper.EXT);
            
}
            
        }
        
        log_message
('debug', 'Helpers loaded: '.implode(', ', $helpers));
    
}


Example of structure in helper files:

if ( ! function_exists('some_function'))
{
    
function some_function()
    
{
        
// some code
    
}
}

if ( ! function_exists('another_function'))
{
    
function another_function'()
    {
        // some code
    }
}

 Signature 

Code Igniter 1.5.4 / CentOS 5 / PHP 5.2.3 / Apache 2.2.2 / MySQL 5.0.27

Profile
 
 
Posted: 13 December 2006 01:11 PM   [ Ignore ]   [ # 1 ]  
Research Assistant
RankRankRank
Total Posts:  970
Joined  04-13-2006

Your input is useful, joeles; but personally I prefer the “helpers as classes” suggestion discussed here.

Profile
 
 
Posted: 13 December 2006 01:24 PM   [ Ignore ]   [ # 2 ]  
Grad Student
Avatar
Rank
Total Posts:  40
Joined  09-20-2006

I agree completely.  I ran into this yesterday, in fact.

Profile
 
 
Posted: 13 December 2006 01:36 PM   [ Ignore ]   [ # 3 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  486
Joined  09-14-2006

LOL, I didn’t realize that someone had already posted a practically identical solution to mine. I’m mostly concerned with being able to customize the helpers so I really could go either way as long as the problem is solved. That being said, for reasons given in the post you pointed to, I would tend toward keeping the procedural. There is really no BIG reason for making them classes and it just introduces another layer of complexity to something that should be really simple. The way Clemens and I have proposed provides exactly the same functionality as making them classes, without the bloat.  But as I said, I would really be ok either way.

 Signature 

Code Igniter 1.5.4 / CentOS 5 / PHP 5.2.3 / Apache 2.2.2 / MySQL 5.0.27

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 719, on June 06, 2008 10:16 AM
Total Registered Members: 66415 Total Logged-in Users: 35
Total Topics: 84756 Total Anonymous Users: 2
Total Replies: 454848 Total Guests: 243
Total Posts: 539604    
Members ( View Memberlist )