Welcome Guest, Not a member yet? Register   Sign In
CamelCase for CI baseclasses: a bit of fun that might be helpfull to someone else
#1

[eluser]Jelmer[/eluser]
I created this just for a bit of fun while I was thinking about making one of my projects a bit more consistent in coding style. An arguement for using underscores instead of camelcase was that every CI baseclass use would break consistency, I wrote this to see if that was easily overcome. In the end I'll probably stick to using underscores but wrote the code anyway for fun.

The following code extends each CI baseclass before instantiating an object. It extends the class with a __call() overloading method that rewrites the requested method from camelcase to the CI default.

Regretably you can't do it entirely through using a MY_ class because you need to extend the load_class() function as well as the loader class, and as far as I know it's not possible to do that.

It's based on CI2 but will likely work in 1.7.x as well. It is purely PHP5 though and cannot work in PHP4.

The load_class() in system/core/Common.php method needs the following code added after the foreach loop that searches BASEPATH en APPPATH for the library and before it looks for a config file:
Code:
if ( $prefix == 'CI_' )
{
    $newclass_name = 'call__' . $name;
    $newclass_code =
        'class ' . $newclass_name . ' extends ' . $name . ' {
            public function __call( $method, $args )
            {
                $method = strtolower( preg_replace( \'/([a-z0-9])([A-Z])/\', \'\\\\1_\\\\2\', $method ) );
                
                if ( ! method_exists( $this, $method ) )
                    throw new Exception( \'Invalid method called on ' . $name . ': \' . $method . \'()\' );
                
                return call_user_func_array( array( $this, $method), $args );
            }
        }';
    eval( $newclass_code );
    
    $name = $newclass_name;
}

The Loader class needs extention as well, this can best be done through creating a MY_Loader class with the following function (you'll recognize the exact same code as above as the part added):
Code:
function _ci_init_class($class, $prefix = '', $config = FALSE, $object_name = NULL)
{    
    // Is there an associated config file for this class?  Note: these should always be lowercase
    if ($config === NULL)
    {
        // We test for both uppercase and lowercase, for servers that
        // are case-sensitive with regard to file names
        if (file_exists(APPPATH.'config/'.strtolower($class).EXT))
        {
            include_once(APPPATH.'config/'.strtolower($class).EXT);
        }            
        elseif (file_exists(APPPATH.'config/'.ucfirst(strtolower($class)).EXT))
        {
            include_once(APPPATH.'config/'.ucfirst(strtolower($class)).EXT);
        }
    }
    
    if ($prefix == '')
    {            
        if (class_exists('CI_'.$class))
        {
            $name = 'CI_'.$class;
        }
        elseif (class_exists(config_item('subclass_prefix').$class))
        {
            $name = config_item('subclass_prefix').$class;
        }
        else
        {
            $name = $class;
        }
    }
    else
    {
        $name = $prefix.$class;
    }
    
    if ( $prefix == 'CI_' )
    {
        $newclass_name = 'call__' . $name;
        $newclass_code =
            'class ' . $newclass_name . ' extends ' . $name . ' {
                public function __call( $method, $args )
                {
                    $method = strtolower( preg_replace( \'/([a-z0-9])([A-Z])/\', \'\\\\1_\\\\2\', $method ) );
                    
                    if ( ! method_exists( $this, $method ) )
                        throw new Exception( \'Invalid method called on ' . $name . ': \' . $method . \'()\' );
                    
                    return call_user_func_array( array( $this, $method), $args );
                }
            }';
        eval( $newclass_code );
        
        $name = $newclass_name;
    }
    
    // Is the class name valid?
    if ( ! class_exists($name))
    {
        log_message('error', "Non-existent class: ".$name);
        show_error("Non-existent class: ".$class);
    }
    
    // Set the variable name we will assign the class to
    // Was a custom class name supplied?  If so we'll use it
    $class = strtolower($class);
    
    if (is_null($object_name))
    {
        $classvar = ( ! isset($this->_ci_varmap[$class])) ? $class : $this->_ci_varmap[$class];
    }
    else
    {
        $classvar = $object_name;
    }

    // Save the class name and object name        
    $this->_ci_classes[$class] = $classvar;

    // Instantiate the class        
    $CI =& get_instance();
    if ($config !== NULL)
    {
        $CI->$classvar = new $name($config);
    }
    else
    {        
        $CI->$classvar = new $name;
    }    
}

But as I said, I created it as a bit of fun and I wouldn't recommend it unless you REALLY want camelcase only code.

After this function calls like below should work fine:
Code:
$this->session->setUserdata('someVar', 'someValue');
$this->uri->uriSegment(2);
$this->input->ipAddress();




Theme © iAndrew 2016 - Forum software by © MyBB