Welcome Guest, Not a member yet? Register   Sign In
Extending Controller via Library
#1

[eluser]mmarcus44[/eluser]
I want to extend the main controller with one of my libraries without calling the super object into a variable.

Is there a way I can give one of my libraries a parent::controller(); and have it extend the controller and use $this-> instead of $ci->......

I just want to be able to access all the CI resources such as database and loading and models etc without having to worry about if I use $ci-> everytime.

Mike
#2

[eluser]Colin Williams[/eluser]
You have no architectural goals with this? You just want to be lazy? You could always just create MY_Controller.php and put your library functions there. Then your app will be both lazy-minded and disorganized Smile
#3

[eluser]mmarcus44[/eluser]
Well the goal is to be able to use a function within a library from any other controller by extending the main controller, not loading the super object into a variable then using that object.

I think this is actually more organized. Should I not be able to use a function that has all the resources from any of my main controllers?

example:

I have 2 controllers admin.php and user.php..... im running a function in admin.php that would want to use a function from user.php... I can't just load the user object into my admin object, I have to load a library that can then run that specific function that can be shared..... but I cant do that either becuase I have to load the entire CI super object first and use $ci->function() rather than $this->function().

I want to be able to EXTEND the main controller, why is this disoragnized?

Can I just load the super object into the library and then in the library constructor I can just pick apart the resources I want from the main controller and reassign them all over again?

Like this:

Code:
class Library{

var $db;
var $load;
var $encrypt;
    
    function Library(){        
        $ci =& get_instance();
        $this->db = $ci->db;
        $this->load = $ci->load;
        $this->encrypt = $ci->encrypt;
    }
}
#4

[eluser]Colin Williams[/eluser]
Quote:Can I just load the super object into the library and then in the library constructor I can just pick apart the resources I want from the main controller and reassign them all over again?

Absolutely. I do that all the time, in fact. Pardon me for not suggesting that.

If you are on PHP 4, you'll need to be sure to assign them all by reference (=& and not just =). PHP 5 will assign references by default.




Theme © iAndrew 2016 - Forum software by © MyBB