Welcome Guest, Not a member yet? Register   Sign In
Never use get_instance() again - Introducing Core
#1

[eluser]wiredesignz[/eluser]
It's all very simple really, autoload this plugin and extend your own Libraries from Core. Never use get_instance() ever again.
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* CodeIgniter Core Extender Plugin
*
* Adapted from Modular Extensions - HMVC
*
* Description:
* This Plugin is extended by your own libraires to obtain an instance
* of the CodeIgniter core.
*
* Your libraries then have access to the currently loaded core classes
* in the same manner as a controller.
*
* Install this file as application/plugins/core_pi.php
* and set $autoload['plugin'] = array('core');
*
* @version: 0.1 (c) Wiredesignz 2008-04-17
**/

class Core
{
    /** Constructor **/
    function Core()
    {
        $ci =& get_instance();
        
        $this->_assign_libraries($ci);
    }
    
    /** Assign core libraries **/
    function _assign_libraries(&$_parent)
    {        
        foreach (array_keys(get_object_vars($_parent)) as $key)
        {
            if (!isset($this->$key) AND $key != get_class($this))
            {
                $this->$key =& $_parent->$key;
            }
        }
    }
}
I should have thought of this a long time ago.
#2

[eluser]Jamie Rumbelow[/eluser]
Helpful, but I prefer to use get_instance. Therefore I can easily seperate CI's from my code.
#3

[eluser]wiredesignz[/eluser]
You use get_instance() to access the CI core, this plugin brings the core to you. The benefits are obvious.

But of course everyone is free to choose.
#4

[eluser]m4rw3r[/eluser]
How about properties being overwritten when you save data?
I don't think all libs, models, etc. are needed in every class (model, lib, whatever), and personally I load the CI obj when I need it (and it doesn't mess up the results from print_r() Smile ).
If you load the obj when you need it, and don't save it as a property, you can name all properties whatever you want without fear of overwriting a lib or something.

Of course this is a good thing if you want to make a sub_controller (HMVC?) or something similar (or if you just want to be lazy ;-) ).
#5

[eluser]wiredesignz[/eluser]
You can still over-write controller properties just as easily, if you aren't careful, this is no different.

To me this is more efficient coding. Lazy is good Wink
#6

[eluser]frenzal[/eluser]
so in stead of $this->obj->somelib I would use $this->core->somelib?
#7

[eluser]wiredesignz[/eluser]
Just $this->somelib.

Everything available to your Controller is available to libraries extended from Core.

ie: $this->uri, $this->load, $this->some_model.

However loading additional libraries inside a library may be an issue and is not recommended.




Theme © iAndrew 2016 - Forum software by © MyBB